160 likes | 415 Views
Strings. string.h library. String Library Functions. #include < string.h >. 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 s1[15] = “Hello”; char s2[15] = “My name is:”;
E N D
Strings string.h library
String Library Functions #include <string.h> CENG 114
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 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 */ CENG 114
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:”; char s3[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 */ CENG 114
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:”; char s3[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 */ CENG 114
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”; char s3[40]; strcpy(s3, s1); /* s3 contains Hello\0 */ strcat(s3, “ “); /* s3 contains Hello \0 */ strcat(s3, s2); /* s3 contains Hello John\0 */ CENG 114
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”; char s3[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 */ CENG 114
strcmp • strcmp(char *s1, char *s2) • Input: Pointers to two strings • Output: compares s1 to s2 alphabetically and returns • 0 if identical, • a negative value if s1 comes before s2, • a positive value if s2 comes before s1 • Example: int n; char s1[15] = “Can”; char s2[15] = “Caner”; char s3[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 */ CENG 114
strncmp • strncmp(char *s1, char *s2, int n) • Input: Pointers to two strings and an integer • Output: compares the first n chars of s1 to s2 alphabetically and returns • 0 if identical, • a negative value if s1 comes before s2, • a positive value if s2 comes before s1 • Example: int n; char s1[15] = “Can”; char s2[15] = “Caner”; char s3[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 */ CENG 114
strstr • strstr(char *s1, char *s2) • 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"); CENG 114
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); CENG 114
strtok • strtok(char *s1, 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. • Example: • char s1[] = “Can-Tan-Betul”; • strtok(s1, “-”); • strtok(NULL, “-”); • strtok(NULL, “-”); CENG 114
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" CENG 114
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" CENG 114
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" CENG 114
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" CENG 114