The header file <string.h> in C includes various string manipulation functions. Some common functions are strlen, strcpy, strcmp, strcat, strlwr, and strrev.
strlen Function
The strlen function returns the length of a string. It takes the string name as an argument. The syntax for using strlen is:
n = strlen(str);
Here, str is the name of the string, and n is the length of the string returned by the strlen function.
strcpy Function
In C, you cannot simply assign one character array to another; you have to copy element by element. The string library <string.h> contains a function called strcpy for this purpose. The strcpy function is used to copy one string to another. The syntax is:
strcpy(str1, str2);
Here, str1 and str2 are two strings. The content of string str2 is copied onto string str1
strcmp Function
The strcmp function compares two strings character by character and stops comparison when there is a difference in the ASCII value or the end of any one string. It returns the ASCII difference of the characters as an integer. A return value of zero means the two strings are equal, a negative value means the first string is less than the second, and a positive value means the first string is greater than the second. The syntax is:
n = strcmp(str1, str2);
Here, str1 and str2 are the two strings to be compared, and n is the returned value of the differed characters.
strcat Function
The strcat function is used to join one string to another. It takes two strings as arguments; the characters of the second string are appended to the first string. The syntax is:
strcat(str1, str2);
Here, str1 and str2 are the two string arguments, and string str2 is appended to string str1.
strlwr Function
The strlwr function converts upper case characters of a string to lower case characters. The syntax is:
strlwr(str1);
Here, str1 is the string to be converted into lower case characters.
strrev Function
The strrev function reverses the given string. The syntax is:
strrev(str);
Here, the string str will be reversed.