Strings string h library String Library Functions include
Strings string. h library
String Library Functions #include <string. h> Name Description strlen return the length of string not counting 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 Dr. Sadık Eşmelioğlu CENG 114 2
strlen • strlen(char *s) – Input: Pointer to a string array – Output: Returns the number of chars in array until the null character – Example: int N; char s 1[15] = “Hello”; char s 2[15] = “My name is: ”; N = strlen(s 1); /* N is 5 */ N = strlen(s 2); /* N is 11 */ N = strlen(“How are you? ”); /* N is 12 */ Dr. Sadık Eşmelioğlu CENG 114 3
strcpy • strcpy(char *dest, char *src) – Input: Pointers to destination and source char arrays – Output: Copies src string into dest string – Example: char s 1[15] = “Hello”; char s 2[15] = “My name is: ”; char s 3[40]; strcpy(s 3, s 1); /* s 3 contains Hello */ strcpy(s 3, s 2); /* s 3 contains My Name is: */ strcpy(s 3, “How are you? ”); /* s 3 contains How are you? */ Dr. Sadık Eşmelioğlu CENG 114 4
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 s 1[15] = “Hello”; char s 2[15] = “My name is: ”; char s 3[40]; strncpy(s 3, s 1, 7); strncpy(s 3, s 2, 7); strncpy(s 3, “How are you? ”, 7); Dr. Sadık Eşmelioğlu CENG 114 /* s 3 contains Hello */ /* s 3 contains My Name */ /* s 3 contains How are */ 5
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 s 1[15] = “Hello”; char s 2[6] = “John”; char s 3[40]; strcpy(s 3, s 1); /* s 3 contains Hello strcat(s 3, “ “); /* s 3 contains Hello strcat(s 3, s 2); /* s 3 contains Hello John Dr. Sadık Eşmelioğlu CENG 114 */ */ */ 6
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 s 1[15] = “Hello”; char s 2[6] = “John”; char s 3[40]; strcpy(s 3, s 1); strcat(s 3, “ “); strncat(s 3, s 2, 6); strncat(s 3, s 1, 3); Dr. Sadık Eşmelioğlu /* s 3 contains CENG 114 Hello Hello John Hello John. Hel */ */ 7
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 s 1 comes before s 2, • a positive value if s 2 comes before s 1 – Example: intn; char s 1[15] = “Can”; char s 2[15] = “Caner”; char s 3[15] = “Cem”; n = strcmp(s 1, “Can”); n = strcmp(s 1, s 2); n = strcmp(s 2, s 1); n = strcmp(s 1, s 3); Dr. Sadık Eşmelioğlu /* n is zero /* n is negative /* n is positive /* n is negative CENG 114 */ */ 8
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 s 1 comes before s 2, • a positive value if s 2 comes before s 1 – Example: int n; char s 1[15] = “Can”; char s 2[15] = “Caner”; char s 3[15] = “Cem”; n = strncmp(s 1, s 2, 4); n = strncmp(s 1, s 2, 3); n = strncmp(s 1, s 3, 3); Dr. Sadık Eşmelioğlu /* n is negative /* n is zero /* n is negative CENG 114 */ */ */ 9
strstr • strstr(char *s 1, char *s 2) – Input: Pointers to two strings – Output: searches for s 2 in s 1 and returns • a pointer to the starting location of s 2 in s 1, • NULL pointer if s 2 cannot be found in s 1 – Example: char str 1[25] = "I cannot stay long"; char str 2[25]; printf("Enter a string: "); scanf("%s", str 2); if(strstr(str 1, str 2) == 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ğlu CENG 114 10
strstr – Example 2: char str 1[25] = "I cannot stay long"; char str 2[25]; char *ret; printf("Please enter a four-letter verb: "); scanf("%s", str 2); ret = strstr(str 1, "stay"); strncpy(ret, str 2, 4); puts(str 1); Dr. Sadık Eşmelioğlu CENG 114 11
strtok • strtok(char *s 1, char *delim) – Input: Pointers to two strings. s 1 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 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. Example: char s 1[] = “Can-Tan-Betul”; strtok(s 1, “-”); strtok(NULL, “-”); Dr. Sadık Eşmelioğlu 0 1 2 3 4 5 6 7 8 9 10 11 12 13 C a n - T a n - B e t u l C a n T a n B e t u l CENG 114 12
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); } Output: result is “Hearts" result is “Spades" result is “Diamonds" result is “Clubs" Dr. Sadık Eşmelioğlu CENG 114 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); } Output: result is "Hearts-" result is "-Spades-" result is "-Diamonds-" result is "-Clubs" Dr. Sadık Eşmelioğlu CENG 114 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); } Output: result is "Hearts" result is “Spades" result is “Diamonds" result is “Clubs" Dr. Sadık Eşmelioğlu CENG 114 15
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); } Output: result is "January" result is "15" result is "2010" Dr. Sadık Eşmelioğlu CENG 114 16
- Slides: 16