How can you compare the size of strings in the C language?
In the C language, you can use the strcmp function to compare the sizes of two strings. The function prototype is:
int strcmp(const char *str1, const char *str2);
The function will return an integer value; it will return a negative number if str1 is less than str2, 0 if they are equal, and a positive number if str1 is greater than str2. For example:
char str1[] = "Hello";
char str2[] = "World";
if(strcmp(str1, str2) < 0) {
printf("str1 is smaller than str2\n");
} else if(strcmp(str1, str2) > 0) {
printf("str1 is larger than str2\n");
} else {
printf("str1 is equal to str2\n");
}
Note: When comparing strings, the strcmp function will compare the characters in the strings one by one until a different character is encountered or the end of the string ‘\0’ is reached.