strcasecmp in c
The strcasecmp
function in C is used to compare two strings, ignoring the case of the characters. It returns an integer value that indicates the result of the comparison. If the strings are equal (ignoring case), it returns 0. If the first string is greater than the second string, it returns a positive value. If the first string is less than the second string, it returns a negative value.
Here is an example usage of the strcasecmp
function:
#include <stdio.h>
#include <strings.h>
int main() {
char str1[] = "Hello";
char str2[] = "hello";
int result = strcasecmp(str1, str2);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result > 0) {
printf("The first string is greater than the second string.\n");
} else {
printf("The first string is less than the second string.\n");
}
return 0;
}
In this example, the strcasecmp
function is used to compare the strings "Hello" and "hello". Since the strings are equal (ignoring case), the output will be "The strings are equal."
Note that the strcasecmp
function is case-insensitive and may not be available on all systems. In such cases, the stricmp
function can be used as an alternative.