Check if Two Strings Are Equal in C
In C, we can compare two strings to check if they are equal using functions like strcmp()
from the string.h
library or by manually comparing each character using a loop.
In this tutorial, we will explore multiple ways to check if two strings are equal in C with detailed explanations and examples.
Examples to Compare Two Strings
1. Using strcmp()
Function to Check if Strings Are Equal
In this example, we will use the strcmp()
function from the string.h
library to compare two strings.
The strcmp()
function returns:
- 0 if the strings are equal
- A positive value if the first string is greater than the second
- A negative value if the first string is less than the second
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("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
Explanation:
- We declare two character arrays
str1
andstr2
with the same value “Hello”. - We use the
strcmp()
function to comparestr1
andstr2
. - Since both strings are identical,
strcmp()
returns 0. - We check if the return value is 0 and print “The strings are equal.”
Output:
The strings are equal.
2. Comparing Strings Character by Character
In this example, we will manually compare each character of the strings using a loop to check if they are equal.
main.c
#include <stdio.h>
int areStringsEqual(char str1[], char str2[]) {
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return 0; // Strings are not equal
}
i++;
}
return (str1[i] == '\0' && str2[i] == '\0'); // Both should end together
}
int main() {
char str1[] = "World";
char str2[] = "World";
// Manually comparing strings
if (areStringsEqual(str1, str2)) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
Explanation:
- We define a function
areStringsEqual()
that takes two strings as arguments. - We use a loop to iterate through each character of both strings.
- If any character is different, we return 0 (strings are not equal).
- If we reach the end of both strings simultaneously, we return 1 (strings are equal).
- In the
main()
function, we callareStringsEqual()
to comparestr1
andstr2
.
Output:
The strings are equal.
3. Using strncmp()
for Limited Comparison
In this example, we will use strncmp()
to compare only the first n
characters of two strings.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Programming";
char str2[] = "Program";
// Comparing first 7 characters
if (strncmp(str1, str2, 7) == 0) {
printf("The first 7 characters are equal.\n");
} else {
printf("The first 7 characters are not equal.\n");
}
return 0;
}
Explanation:
- We declare two strings
str1
andstr2
with different lengths. - We use
strncmp()
to compare only the first 7 characters of both strings. - If the first 7 characters match, it returns 0, meaning they are equal.
- Since “Programming” and “Program” have the same first 7 characters, we print “The first 7 characters are equal.”
Output:
The first 7 characters are equal.
Conclusion
In this tutorial, we covered different ways to check if two strings are equal in C:
- Using
strcmp()
for full comparison. - Manually comparing characters using a loop.
- Using
strncmp()
for partial comparison.