0
8.8kviews
Explain any three string standard library functions.
1 Answer
0
379views

I) strlen(string):

The length of string is equal to the number of characters present in the string, excluding null characters. In short it finds the length of strings.

Example:

Char str[20] = “John Terry”;
Int n;
N = strlen(str);

II) strcopy(string1, string2):

String 2 is copied to string 1. String 2 can be a character array or character constant. We should not use assignment statement.

Example:

Char string1[20], string2[20] = “Facebook”
String1 = string2; /* This is invalid */
Strcopy(string1, string2); /* This is valid */
Strcopy(string1, “Gmail”);

III) strcmp(string1, string2):

It compares one string to another. strcmp performs an unsigned comparison of string1 to string2, starting with the first character in each string and continuing with subsequent characters until the corresponding characters differ or until the end of the strings is reached.

The strcmp function returns zero, if string1 = string2, negative value if string1 < string 2 and positive value if string1 > string2.

Example:

int main (){
Char str1[10] = “Facebook”, str2[10] = “Yahoo”,
Char str3[10] = “Outlook”, str4[10] = “Yahoo”;
Diff = strcmp(str1, str2); /* str1 is less than str2 */
printf(Diff); /* Diff is -1 */

Diff = strcmp(str2, str3); /* str2 is greater than str3 */
printf(Diff); /* Diff is 10 */

Diff = strcmp(str2, str4); /* str2 is equal to str4 */

printf(“Diff”); /* Diff is 0 */

}
Please log in to add an answer.