Introduction
We compare strings to check if two words or sentences are the same or to find which one comes first in order.
#include <string.h> int strcmp(const char *str1, const char *str2);
#include <string.h> int result = strcmp("apple", "apple");
int result = strcmp("apple", "banana");
int result = strcmp("banana", "apple");
#include <stdio.h> #include <string.h> int main() { char str1[] = "hello"; char str2[] = "hello"; char str3[] = "world"; int res1 = strcmp(str1, str2); int res2 = strcmp(str1, str3); printf("Compare str1 and str2: %d\n", res1); printf("Compare str1 and str3: %d\n", res2); if (res1 == 0) { printf("str1 and str2 are the same.\n"); } else { printf("str1 and str2 are different.\n"); } if (res2 < 0) { printf("str1 comes before str3.\n"); } else if (res2 > 0) { printf("str1 comes after str3.\n"); } else { printf("str1 and str3 are the same.\n"); } return 0; }
strcmp function in C?#include <stdio.h>
#include <string.h>
int main() {
char a[] = "apple";
char b[] = "apricot";
int result = strcmp(a, b);
if (result < 0) {
printf("a is less than b\n");
} else if (result > 0) {
printf("a is greater than b\n");
} else {
printf("a and b are equal\n");
}
return 0;
}#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = "hello";
if (str1 == str2) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}str1 and str2 are equal ignoring case differences in C. Which approach correctly achieves this?Pass the test and this pattern moves unknown