Chapter 5 Section 5.4 The Common String Library Functions CS 50 Hathairat Rattanasook
Library Functions We already discussed the library function fgets() Library functions are available: to find the length of a string: strlen() to make a copy of a string: strcpy() to concatenate one string to the end of another: strcat() to compare two strings: strcmp() CS 50 - Hathairat Rattanasook 1
strlen() It calculates the length of a string. The first parameter for strlen() is the address of a string, that is the first char of a null-terminated string. The return type is size_t which is a type in ANSI C that is used to specify sizes or lengths in bytes. It is compatible with the integer types. CS 50 - Hathairat Rattanasook 2
strlen() - Example 1 char *str = "This is a string"; 2 int length; 3 length = strlen("hello World!"); 4 printf("length: %d\n", length); // prints 12 5 length = strlen(str); 6 printf("length: %d\n", length); // prints 16 Line 3: Returns the length of "Hello World!" which is 12 Line 5: Returns the length of str pointer which is 16 CS 50 - Hathairat Rattanasook 3
strcpy() It makes a copy of a string. It takes two parameters: The first parameter is the address of a char array where the copied string will be written to. The memory space has to be allocated before calling this function. The second parameter is the first char of a nullterminated string. WARNING: strcpy() requires that enough memory has been allocated for the first parameter! CS 50 - Hathairat Rattanasook 4
strcpy() - Example 1 char copy[64]; 2 char *original = "C is fun!"; 3 strcpy(copy, original); 4 printf("%s\n", copy); 5 strcpy(copy, "C++ is fun too!"); 6 printf("%s\n", copy); Line 3: Copies the string original to the char array copy. Line 5: Copies the "C++ is fun too!" to char array copy. NOTE: The copy variable is declared as an array which allocates memory for 64 characters including the null terminated character. CS 50 - Hathairat Rattanasook 5
strcat() It appends one string to the end of another string. It takes two parameters: The first parameter is the address of an array which contains a null-terminated string. The array must be allocated with enough free space to fit the string that is passed in as the second parameter. The second parameter is the first char of a nullterminated string which will be appended to the first parameter. CS 50 - Hathairat Rattanasook 6
strcat() - Warning WARNING: Just like strcpy() and strcat() requires that enough memory has been allocated for the first parameter! If there is not enough free space in the first parameter to fit the second string, strcat() will continue to write to memory and deleting whatever is currently at that memory location. CS 50 - Hathairat Rattanasook 7
strcat() - Example 1 char str[64] = "one two"; 2 strcat(str, "three four"); 3 printf("%s\n", str); Line 1: Creates a char array containing "one two" and additional space allocated to fit another string Line 2: Concatenates the string "three four" to the string "one two" that is provided as first argument. Line 3: prints "one twothree four" NOTE: The str variable is declared as an array which allocates memory for 64 characters CS 50 - Hathairat Rattanasook 8
It compares two strings. It takes two parameters: strcmp() The first parameter is a char pointer of a null-terminated string. The second parameter is also a char pointer of a null-terminated string. The return value is: 0 if both strings are identical A negative number if the first string is lexicographically less than the second string A positive number if the first string is lexicographically greater than the second string CS 50 - Hathairat Rattanasook 9
strcmp() - Example 1 char *str = "Hello", *str2 = "Hello"; 2 int result = strcmp("this is a string", "This is a STRING"); 3 printf("%d\n", result); 4 result = strcmp(str, str2); 5 printf("%d\n", result); Line 2: compares two strings which differ by the capital word STRING. Line 3: Prints 32 because the first string is lexicographically greater than the second string Line 5: Prints 0 which means that both strings are equal. CS 50 - Hathairat Rattanasook 10
Question? 1. What is the difference between? char *str = "Some string"; char str[64] = "Some string"; 2. Can you concatenate "abc" and "def"? 3. What does it mean if strcmp() returns a negative number? CS 50 - Hathairat Rattanasook 11
Answers 1. What is the difference between? char *str = "Some string"; // fixed sized string char str[64] = "Some string"; /* string with "Some string" but additional allocated free memory space to fit additional chars */ 2. Can you concatenate "abc" and "def"? char abc[7] = "abc", *def = "def"; strcat(abc, def); printf("%s\n", abc); 3. What does it mean if strcmp() returns a negative number? A negative number means the first string is lexicographically less than the second string, eg. strcmp("abc", "def") return -3 CS 50 - Hathairat Rattanasook 12