Compare Two Strings in C
In C, we can compare two strings using the strcmp()
function from the string.h
library or by manually comparing characters using loops. These methods help determine whether two strings are equal, or which one is lexicographically greater.
String Comparison is commonly used in various applications like user input validation, searching, and sorting strings.
In this tutorial, we will explore different ways to compare two strings with practical examples.
Examples of Comparing Two Strings in C
1. Comparing Two Strings Using strcmp()
In this example, we will use the strcmp()
function from string.h
to compare two strings and check if they are equal or different.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
// Using strcmp() to compare strings
if (strcmp(str1, str2) == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
Explanation:
- We declare two character arrays,
str1
andstr2
, and initialize them with the string"Hello"
. - We use the
strcmp()
function, which comparesstr1
andstr2
character by character. - If the function returns
0
, it means both strings are identical. - If it returns a nonzero value, the strings are different.
Output:
Strings are equal.
2. Comparing Two Strings Without strcmp()
In this example, we will compare two strings manually using a loop, without using the strcmp()
function.
main.c
#include <stdio.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int i = 0, isEqual = 1;
// Manually comparing each character
while (str1[i] != '\0' || str2[i] != '\0') {
if (str1[i] != str2[i]) {
isEqual = 0;
break;
}
i++;
}
if (isEqual) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
Explanation:
- We declare two character arrays,
str1
andstr2
, and assign them different values. - We initialize a loop to traverse both strings character by character.
- We compare each character; if they are different, we set
isEqual
to0
and exit the loop. - If all characters match,
isEqual
remains1
and we print “Strings are equal”. Otherwise, we print “Strings are not equal”.
Output:
Strings are not equal.
3. Case-Insensitive String Comparison Using strcasecmp()
In this example, we will compare two strings without considering case differences using the strcasecmp()
function.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = "HELLO";
// Using strcasecmp() for case-insensitive comparison
if (strcasecmp(str1, str2) == 0) {
printf("Strings are equal (ignoring case).\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
Explanation:
- We declare two character arrays
str1
andstr2
, with different cases. - We use the
strcasecmp()
function, which performs a case-insensitive string comparison. - If the function returns
0
, the strings are identical, ignoring case. - If the function returns a nonzero value, they are different.
Output:
Strings are equal (ignoring case).
Conclusion
In this tutorial, we explored different ways to compare two strings in C:
- Using
strcmp()
: Standard method for comparing two strings. - Manual Comparison: Useful when avoiding built-in functions.
- Using
strcasecmp()
: For case-insensitive comparison.