How do you compare two strings in C?
In C programming, you can compare two strings using several methods. The standard C library provides string comparison functions that allow you to compare strings based on their lexicographical order. Here's a detailed explanation of how to compare strings in C with examples.
Compare two strings in C |
1. strcmp():
The "strcmp()" function is commonly used to compare two strings in C. It returns an integer value indicating the result of the comparison. The return value is 0 if the strings are equal, a negative value if the first string is lexicographically smaller, and a positive value if the first string is lexicographically larger.
Here's an example using "strcmp()" to compare two strings:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is smaller than str2\n");
} else {
printf("str1 is larger than str2\n");
}
return 0;
}
In this example, the "strcmp()" function is used to compare "str1" and "str2". The result is stored in the "result" variable, and then it is checked to determine the relationship between the two strings.
2. strncmp():
The "strncmp()" function is similar to "strcmp()", but it allows you to specify the maximum number of characters to compare. It is useful when you want to compare only a portion of the strings.
Here's an example using "strncmp()" to compare a portion of two strings:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[] = "Hello, Everyone!";
int result = strncmp(str1, str2, 6); // Compare only the first 6 characters
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is smaller than str2\n");
} else {
printf("str1 is larger than str2\n");
}
return 0;
}
In this example, "strncmp()" is used to compare the first 6 characters of "str1" and "str2". The comparison is then performed based on the specified number of characters.
3. Custom Comparison:
If you need to perform a custom comparison based on specific criteria, you can write your own comparison function using loops and conditional statements. This allows you to compare strings based on different rules or conditions.
Here's an example of a custom string comparison function:
#include <stdio.h>
int compareStrings(char str1[], char str2[]) {
int i = 0;
while (str1[i] == str2[i]) {
if (str1[i] == '\0')
return 0;
i++;
}
return str1[i] - str2[i];
}
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = compareStrings(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is smaller than str2\n");
} else {
printf("str1 is larger than str2\n");
}
return 0;
}
In this example, the "compareStrings()" function compares two strings character by character until a difference is found. It returns 0 if the strings are equal, a negative value if the first string is smaller, and a positive value if the first string is larger.
These are the common methods to compare strings in C. It's important to note that string comparisons in C are based on lexicographical order, and understanding the return values of string comparison functions is crucial for proper handling of string comparisons in your programs.