#include <stdio.h>
#include <string.h>
int main(void){
char str1[]="Plus2";
char str2[]="plus2net";
char str3[]="Plus2net";
int x,y,z;
x=strcmpi(str1,str2);
y=strcmpi(str2,str1);
z=strcmpi(str2,str3);
printf("Output: %d , %d,%d",x,y,z);
return 0;
}
The output of above code is here
-1,1,0
x =-1 because length of str1 is less than length of str2. strcmpi(string1, string2);
string1, string2 : Two Input strings for camparison. #include <stdio.h>
#include <string.h>
int main(void){
char str2[]="plus2net";
char str3[]="Plus2net"; // Watch the first char P
int z;
z=strcmpi(str2,str3);
printf("Output: %d",z);
return 0;
}
Output
0
Note : this ignores case of the letter for comparison.
This example demonstrates how to compare two strings without considering case sensitivity using strcmpi().
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "hello";
if (strcmpi(str1, str2) == 0) {
printf("The strings are equal (case-insensitive).\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
Output:
The strings are equal (case-insensitive).
---
In environments where strcmpi() is unavailable, strcasecmp() can be used as an alternative for case-insensitive comparisons.
#include <stdio.h>
#include <strings.h>
int main() {
char str1[] = "Plus2Net";
char str2[] = "plus2net";
if (strcasecmp(str1, str2) == 0) {
printf("The strings are equal (case-insensitive).\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
Output:
The strings are equal (case-insensitive).
---
This example compares two strings entered by the user, ignoring case sensitivity.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50], str2[50];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0'; // Remove newline character
if (strcmpi(str1, str2) == 0) {
printf("The strings are equal (case-insensitive).\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
Output:
Enter the first string: Test
Enter the second string: test
The strings are equal (case-insensitive).