STRINGS PART II (STRING FUNCTIONS) by James Tam (You can find a version of these notes in Unix under: /home/profs/tamj/233/examples/c_examples/string2) The string functions that are provided with the header file string.h are described below. The program string_functions.c (found in the directory /home/profs/tamj/233/examples/c_examples/string) provides some accompanying code. 1) String length. Syntax: strlen(string) This function will return a number that is equal to the length of the string in bytes which can the be stored in a variable. eg. - int no; char string[6] = "hello"; no = strlen(string); In this example number will be equal to 5 bytes since the final null character at the end of the string is not counted. 2) String compare a) Compare the characters in two strings to determine which one comes first alphabetically. Syntax: strcmp(string1,string2) This function will compare two strings and return a number depending if the string1 appears alphabetically before or after string2 (conceptually picture that it performs a character-by-character subtraction of string2 from string1). if (string1 before string2) strmp(string1,string2) < 0 if (string1 after string2) strcmp(string1,string2) > 0 If the strings are identical then the number returned is zero. Again, like the strlen function you can store the number returned in a variable. e.g., int no; char string1[10] = "abc"; char string2[10] = "def"; no = strcmp(string1,string2); In this example "no" will be a negative value since string1 would come before string2 in the dictionary (assuming that both were real words). If the strings are of unequal length then the number of bytes that are compared is equal to the length of the shortest string. b) Compare only the first n characters of the strings. Syntax: strncmp(string1,string2,n) Again, this function returns a number that is the result of subtracting the ASCII values of string2 from string1. If strncmp reaches the end of either string after comparing "m" bytes where m < n (i.e., the null character has been reached) then strncmp will stop. The determination of which string comes first will be based only the first m bytes. 3) String concatenation a) Append the second string onto the end of the first string. Syntax: strcat(string1,string2) It takes string2 and appends it to the end of string1. Then a null character is appended onto the end of the now lengthened string1. The programmer is the one who must be reponsible for ensuring that string1 is long enough to accomodate the new characters addded on by string2 (you may overwrite the memory locations that come after string1 or cause a segmentation fault to oocur as your program accesses memory segments that it doesn't have the permission level to). b) Appending the first n characters of the second string onto the end of the first string. Syntax: strncat(string1,string2,n) Similar to strcat but only the first n characters of string2 are concatenated onto the end of string1. 4) String copy a) Copying string 2 into string 1 (including the null character). Syntax: strcpy(string1,string2) The contents of string2 is copied into string1 including the null character. Whatever was in string1 may be lost forever. Again, it is the job of the programmer to ensure that string1 is long enough to hold the contents of string2. b) Copying the first n characters of string2 into string1. Syntax: strncpy(string1,string2,n) Copies the first n characters of string2 into string1 or until a null has been copied whichever comes first. Whatever was in these first few characters of string1 is lost forever and the remainder of the string1 becomes null.