Comparing strings in C
Track completion, mastery, and revision.
Comparing Strings in C
In C, strings are represented as null-terminated character arrays. Because they are not first-class data types, you cannot compare them directly using standard relational operators like == or >. Instead, string comparison is performed character by character based on their ASCII values.
When comparing two strings, s1 and s2, the comparison yields one of three outcomes:
s1is equal tos2: Every character matches exactly, including the length.s1 > s2: The ASCII value of the first unmatched character ins1is greater than that ins2. Lexicographically (alphabetically),s1comes afters2.s1 < s2: The ASCII value of the first unmatched character ins1is smaller than that ins2. Lexicographically,s1comes befores2.
In this tutorial, we will explore three different approaches to compare strings in C: using built-in library functions, using custom array indexing, and using pointers.
Approach 1: Using the Built-In strcmp() Function
The simplest and most common way to compare strings in C is by using the standard library function strcmp(), which is defined in the <string.h> header.
Syntax
int strcmp(const char *str1, const char *str2);
The strcmp() function compares two strings character by character. The comparison continues until a mismatch is found or the null terminator (\0) is encountered.
Return Values of strcmp()
| Return Value | Description |
| :--- | :--- |
| 0 | Both strings are identical. |
| Negative Integer | The ASCII value of the first unmatched character in str1 is less than that in str2. |
| Positive Integer | The ASCII value of the first unmatched character in str1 is greater than that in str2. |
Note on Case Sensitivity:
ASCII comparisons are case-sensitive. For example, the uppercase letter'H'has an ASCII value of72, while the lowercase letter'h'has an ASCII value of104. Therefore,"Hello"and"hello"are not equal.
Code Implementation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str1, *str2;
size_t size = 100;
printf("Enter first string\n");
str1 = (char*)malloc(size);
getline(&str1, &size, stdin);
printf("Enter second string\n");
str2 = (char*)malloc(size);
getline(&str2, &size, stdin);
if (strcmp(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
free(str1);
free(str2);
return 0;
}
Sample Outputs
Case 1: Strings are equal
Enter first string
hello!
Enter second string
hello!
The strings are equal.
Case 2: Strings are unequal (Case Sensitivity)
Enter first string
Hello!
Enter second string
hello!
The strings are not equal.
Approach 2: Custom Comparison (Without Built-In Functions)
If you cannot use <string.h>, you can implement your own comparison logic.
Algorithm
- Compare Lengths First: As an optimization, check if the lengths of the two strings match. If they do not, they cannot be equal.
- Character-by-Character Iteration: Initialize an index counter
cto traverse both strings simultaneously. - Check for Termination: Loop until a character mismatch is found or the null terminator (
\0) is reached. - Determine Result: If both strings reach the null terminator at the same index, they are equal. Otherwise, compare the mismatched characters to determine which string is lexicographically greater.
Code Implementation
#include <stdio.h>
#include <stdlib.h>
int string_compare(char s1[], char s2[]) {
int c = 0;
int flag = 0;
while (s1[c] == s2[c]) {
if (s1[c] == '\0' || s2[c] == '\0') {
break;
} else {
c++;
}
}
if (s1[c] == '\0' && s2[c] == '\0') {
flag = 1;
return 0;
} else {
if (flag == 0) {
if (s1[c] > s2[c]) {
printf("String 1 is greater than string 2\n");
} else {
printf("String 2 is greater than string 1\n");
}
}
return -1;
}
}
int main() {
char *str1, *str2;
size_t size = 100;
ssize_t l1, l2;
printf("Enter first string\n");
str1 = (char*)malloc(size);
l1 = getline(&str1, &size, stdin); // Storing number of characters read
printf("Enter second string\n");
str2 = (char*)malloc(size);
l2 = getline(&str2, &size, stdin);
// Strip the trailing newline character ('\n') added by getline
l1--;
l2--;
if (l1 == l2) {
if (string_compare(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
} else {
printf("The length of strings are not equal.\n");
}
free(str1);
free(str2);
return 0;
}
Sample Outputs
Case 1: Strings are equal
Enter first string
why?
Enter second string
why?
The strings are equal.
Case 2: Strings are not equal because of length
Enter first string
where are you ?
Enter second string
where are you
The length of strings are not equal.
Case 3: Strings are not equal because of character mismatch
Enter first string
WHY
Enter second string
WHy
String 2 is greater than string 1
The strings are not equal.
Note: The lowercase 'y' in the second string has a higher ASCII value (121) than the uppercase 'Y' (89) in the first string.
Approach 3: Comparing Strings Using Pointers
Pointers offer a highly efficient and idiomatic way to traverse and compare strings in C. Instead of using array indexing (like s1[c]), we can directly dereference the pointers (*str1) and increment them (str1++) to move to the next character.
Code Implementation
#include <stdio.h>
#include <stdlib.h>
int string_compare(char *str1, char *str2) {
while (*str1 == *str2) {
if (*str1 == '\0' || *str2 == '\0') {
break;
}
str1++;
str2++;
}
if (*str1 == '\0' && *str2 == '\0') {
return 0;
} else {
return -1;
}
}
int main() {
char *str1, *str2;
size_t size = 100;
ssize_t l1, l2;
printf("Enter first string\n");
str1 = (char*)malloc(size);
l1 = getline(&str1, &size, stdin);
printf("Enter second string\n");
str2 = (char*)malloc(size);
l2 = getline(&str2, &size, stdin);
// Strip the trailing newline character ('\n')
l1--;
l2--;
if (l1 == l2) {
if (string_compare(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
} else {
printf("The length of strings are not equal.\n");
}
free(str1);
free(str2);
return 0;
}
Sample Outputs
Case 1: Strings are equal
Enter first string
help
Enter second string
help
The strings are equal.
Case 2: Strings are not equal because of length
Enter first string
help
Enter second string
helping
The length of strings are not equal.
Case 3: Strings are not equal because of character mismatch
Enter first string
Hello!
Enter second string
Hellp!
The strings are not equal.
Finished reading?