If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C language, string comparison is the process of comparing two strings to check if they are equal or not. The strcmp()
function is used to compare two strings.
The strcmp()
function is implemented in the <string.h>
library. It compares two strings character by character and returns:
Here is an example of how to use the strcmp()
function:
Copy code
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "world";
int result = strcmp(str1, str2);
if(result == 0) {
printf("Both strings are identical.");
} else if(result > 0) {
printf("String 1 is lexicographically greater than String 2.");
} else {
printf("String 1 is lexicographically less than String 2.");
}
return 0;
}
In the above example, the output will be "String 1 is lexicographically less than String 2."
It's important to note that strcmp()
function is case-sensitive, meaning that "Hello" and "hello" are considered as different strings. If you want to perform case-insensitive string comparison, you can use the strcasecmp()
function.
It's also worth noting that strcmp()
function is not secure and should be avoided if you are working with strings that come from an untrusted source.
Comments: 0