Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.

Similar presentations


Presentation on theme: "Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy."— Presentation transcript:

1 Strings string.h library

2 String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy copies string from source to dest strncopy copies n chars from source to dest strcat appends string from source to end of dest strncat appends n chars from source to end of dest strcmp compares two strings alphabetically strncmp compares the first n chars of two strings strstr finds a string inside another strtok breaks string into tokens using delimiters #include

3 strlen strlen(char *s) – Input: Pointer to a string array – Output: Returns the number of chars in array until the null character – Example: intN; char s1[15] = “Hello”; char s2[15] = “My name is:”; N = strlen(s1);/* N is 5 */ N = strlen(s2);/* N is 11 */ N = strlen(“How are you?”); /* N is 12 */ Dr. Sadık EşmelioğluCENG 1143

4 strcpy strcpy(char *dest, char *src) – Input: Pointers to destination and source char arrays – Output: Copies src string into dest string – Example: char s1[15] = “Hello”; char s2[15] = “My name is:”; chars3[40]; strcpy(s3, s1);/* s3 contains Hello\0 */ strcpy(s3, s2);/* s3 contains My Name is:\0 */ strcpy(s3, “How are you?”); /* s3 contains How are you?\0 */ Dr. Sadık EşmelioğluCENG 1144

5 strncpy strncpy(char *dest, char *src, int n) – Input: Pointers to dest and src char arrays, and an int – Output: Copies the first n chars from src into dest string; if the length of src is less than n, remaining space is filled with null char – Example: char s1[15] = “Hello”; char s2[15] = “My name is:”; chars3[40]; strncpy(s3, s1, 7);/* s3 contains Hello\0\0 */ strncpy(s3, s2, 7);/* s3 contains My Name */ strncpy(s3, “How are you?”, 7); /* s3 contains How are */ Dr. Sadık EşmelioğluCENG 1145

6 strcat strcat(char *dest, char *src) – Input: Pointers to destination and source char arrays – Output: Appends src string into dest string including the null pointer – Example: char s1[15] = “Hello”; char s2[6] = “John”; chars3[40]; strcpy(s3, s1);/* s3 contains Hello\0 */ strcat(s3, “ “);/* s3 contains Hello \0*/ strcat(s3, s2); /* s3 contains Hello John\0*/ Dr. Sadık EşmelioğluCENG 1146

7 strncat strncat(char *dest, char *src, int n) – Input: Pointers to dest and src char arrays, and an integer – Output: Appends the first n chars of the src string to the end of dest string; if the length of src is less than n, only the src is copied with a null char at the end, if more, the first n chars and the null char are appended. – Example: char s1[15] = “Hello”; char s2[6] = “John”; chars3[40]; strcpy(s3, s1);/* s3 contains Hello\0 */ strcat(s3, “ “);/* s3 contains Hello \0*/ strncat(s3, s2, 6); /* s3 contains Hello John\0\0*/ strncat(s3, s1, 3);/* s3 contains Hello JohnHel\0*/ Dr. Sadık EşmelioğluCENG 1147

8 strcmp strcmp(char *s 1, char *s 2 ) – Input: Pointers to two strings – Output: compares s 1 to s 2 alphabetically and returns 0 if identical, a negative value if s1 comes before s2, a positive value if s2 comes before s1 – Example: intn; char s1[15] = “Can”; char s2[15] = “Caner”; chars3[15] = “Cem”; n = strcmp(s1, “Can”);/* n is zero*/ n = strcmp(s1, s2);/* n is negative */ n = strcmp(s2, s1);/* n is positive*/ n = strcmp(s1, s3);/* n is negative*/ Dr. Sadık EşmelioğluCENG 1148

9 strncmp strncmp(char *s 1, char *s 2, int n ) – Input: Pointers to two strings and an integer – Output: compares the first n chars of s 1 to s 2 alphabetically and returns 0 if identical, a negative value if s1 comes before s2, a positive value if s2 comes before s1 – Example: intn; char s1[15] = “Can”; char s2[15] = “Caner”; chars3[15] = “Cem”; n = strncmp(s1, s2, 4);/* n is negative */ n = strncmp(s1, s2, 3);/* n is zero*/ n = strncmp(s1, s3, 3);/* n is negative*/ Dr. Sadık EşmelioğluCENG 1149

10 strstr strstr(char *s 1, char *s 2 ) – Input: Pointers to two strings – Output: searches for s2 in s1 and returns a pointer to the starting location of s2 in s1, NULL pointer if s2 cannot be found in s1 – Example: char str1[25] = "I cannot stay long"; char str2[25]; printf("Enter a string: "); scanf("%s", str2); if(strstr(str1, str2) == NULL) printf("The string entered is not in the sentence!\n"); else printf("The string entered is in the sentence!\n"); Dr. Sadık EşmelioğluCENG 11410

11 strstr – Example 2: char str1[25] = "I cannot stay long"; char str2[25]; char *ret; printf("Please enter a four-letter verb: "); scanf("%s", str2); ret = strstr(str1, "stay"); strncpy(ret, str2, 4); puts(str1); Dr. Sadık EşmelioğluCENG 11411

12 strtok strtok(char *s 1, char *delim) – Input: Pointers to two strings. s1 is the string to be separated into tokens, and delim is a list of separators. – Output: Marks the first non-delim character as the beginning of a token, continues until it reaches a delim character, replaces that character with \ 0 marking the end of the token. Subsequent calls to this function with NULL as the first argument, continues to operate on the same string from the point first call left off. Returns a pointer to the beginning of the token. For subsequent calls with NULL as the first argument, if no delimiter is found, returns NULL. Dr. Sadık EşmelioğluCENG 11412 Example: char s1[] = “Can-Tan-Betul”; strtok(s1, “-”); strtok(NULL, “-”); 012345678910111213 Can-Tan-Betul\0 Can Tan-Betul Can Tan Betul Can Tan Betul

13 strtok Example: char str[] = "Hearts#Spades#Diamonds#Clubs"; char delims[] = "#"; char *result; result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } Dr. Sadık EşmelioğluCENG 11413 Output : result is “Hearts" result is “Spades" result is “Diamonds" result is “Clubs"

14 strtok Example: char str[] = "Hearts-#-Spades-#-Diamonds-#-Clubs"; char delims[] = "#"; char *result; result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } Dr. Sadık EşmelioğluCENG 11414 Output : result is "Hearts-" result is "-Spades-" result is "-Diamonds-" result is "-Clubs"

15 strtok Example: char str[] = "Hearts-#-Spades-#-Diamonds-#-Clubs"; char delims[] = "#-"; char *result; result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } Dr. Sadık EşmelioğluCENG 11415 Output : result is "Hearts" result is “Spades" result is “Diamonds" result is “Clubs"

16 strtok Example: char str[] = “January 15, 2010"; char delims[] = “,"; char *result; result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } Dr. Sadık EşmelioğluCENG 11416 Output : result is "January" result is "15" result is "2010"


Download ppt "Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy."

Similar presentations


Ads by Google