strcmp ( ) in C: A Comprehensive Guide
Learn how to use the built-in strcmp function, as well as how to build your own custom strcmp function from scratch.
Introduction
The strcmp( ) function also called the string compare function is a little bit slightly different from the rest, in that it returns an integer instead of a string of characters.
It is used to compare two strings together and a value in the form of an integer data type is returned based on that comparison. This means that it returns a number, unlike the rest of the string manipulation functions that return a string.
The Built-in strcmp( ) Function
Just like the other string manipulation built-in string function, it is contained in the string.h header file.
This function takes in two arguments: Let us say the s1 and the s2, the two arguments are both strings.
With s1 being the first one, and s2 being the second one, what this function does is take the first argument (s1) and compare it with the second argument (s2). Based on the comparison, it returns the following integer values:
It returns a zero (0) when all characters in s1 are equal to all corresponding characters in s2.
A negative number when the first different character in s1 is less than the corresponding character in s2.
A positive number when the first different character in s1 is greater than the corresponding character in s2.
Let us look at an example of using this function to compare two strings:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *s1;
char *s2;
int num;
s1 = "I Love Solving Programming Problems in C";
s2 = "I Love Programming in C";
printf("Before strcmp = %d\n", num);
num = strcmp(s1, s2);
printf("After strcmp = %d\n", num);
return (0);
}
If we compile and run this code, we will be getting the result as shown below:
Now we will be getting 3, this is so because if you look at the s1, it contains I Love Solving Programming Problems in C
, and if you look at the s2, it says I Love Programming in C
, what the function does is to keep comparing the strings, character by character until it gets to when the character in the first string (s1) is not equal to the corresponding character in the second string (s2), then it takes the ASCII value of the character in s1 and subtracts it with the ASCII value of the character in the s2, then return the result as the answer.
So from what we have, above, the first instance where the character in s1 is not equal to the character in s2 is when it gets to Solving
in s1 and Programming
in s2, now if you see, character S
is not equal to the character P
, thus took the ASCII value of S
, which in C Language is 83 and then subtract from it the ASCII value of P
, which is 80, so we ended up having 3 as our answer.
Building a Custom strcmp( ) Function
To build our strcmp( ) function, we have to consider the behavior of the strcmp( ) inbuilt function that is contained in the string.h header file.
Here are some of the things we have to take care of:
When the characters in the two strings are different, then the ASCII value of the second string should be subtracted from the ASCII value of the first string and the result of the subtraction should be returned.
When all the characters in the two strings are the same or equal, a zero (0) value should be returned.
When all the characters in the two strings are the same, but the first string has additional characters after the first string has reached its null character, then 32 should be returned.
Finally, when all the characters in the two strings are equal, but the second string has additional characters after the first string must have reached its null character, then -32 should be returned.
With all these in mind, I came up with this algorithm to solve all these problems for us to be able to build our strcmp ( ) function.
This is the logic I used and the algorithm I implemented that helped me build one that works perfectly:
Declare variable i - for loop, len - to hold the length of the longest string, len1 - to hold the length of the first string, s1, len2 - to hold the length of the second string, s2.
Initialized len1 and len2 with a 0 value so that it doesn't return an unusual value at the time it is needed.
Used two for-loops, one to find the length of the first string and the second one to find the length of the second string.
Used the ternary operator to check the longer one between the two and store it inside the len variable that we defined earlier.
Using a for-loop, this time around to meet each of the conditions that we mentioned earlier:
If the characters from the two strings in the same position are not the same, subtract the ASCII value of the character in the second string from the corresponding character in the first string. And break, to go out of the loop.
Else-If the characters from the two strings in the same position are the same, then go further and check the following:
If the character of the first string in the next position is a null character and that of the second string in the same next position is not, then return -32.
If the character of the first string in the next position is not a null character and that of the second string in the same position is a null character, then return 32.
If the character of the first string and the corresponding character of the second string are both null characters, then return 0. Then break to go out of the loop.
The code below is a full implementation of both the algorithm that I used to build the strcmp ( ) function from scratch.
int _strcmp(char *s1, char *s2)
{
int i, len, len1 = 0, len2 = 0;
for (i = 0; s1[i] != '\0'; i++)
len1++;
for (i = 0; s2[i] != '\0'; i++)
len2++;
len = len1 > len2 ? len1 : len2;
for (i = 0; i < len; i++)
{
if (s1[i] != s2[i])
{
return (s1[i] - s2[i]);
break;
}
else if (s1[i] == s2[i])
{
if (s1[i + 1] == '\0' && s2[i + 1] != '\0')
{
return (-32);
}
if (s1[i + 1] != '\0' && s2[i + 1] == '\0')
{
return (32);
}
if (s1[i + 1] == '\0' && s2[i + 1] == '\0')
{
return (0);
}
break;
}
}
}
Let us take this example to show how the custom strcmp ( ) function work.
#include <stdio.h>
int _strcmp(char *s1, char *s2);
int main(void)
{
char *s1, *s2, *s3, *s4;
int num, num1, num2, num3, num4;
s1 = "I love software engineering";
s2 = "I love programming in C";
s3 = "I love software engineering";
s4 = "I love software engineering and programming in c";
num = _strcmp(s1, s2);
num1 = _strcmp(s1, s3);
num2 = _strcmp(s2, s3);
num3 = _strcmp(s4, s1);
num4 = _strcmp(s3, s4);
printf("s1 = %s\n", s1);
printf("s2 = %s\n", s2);
printf("s3 = %s\n", s3);
printf("s4 = %s\n\n", s4);
printf("_strcmp(s1, s2) = %d\n", num);
printf("_strcmp(s1, s3) = %d\n", num1);
printf("_strcmp(s2, s3) = %d\n", num2);
printf("_strcmp(s4, s1) = %d\n", num3);
printf("_strcmp(s3, s4) = %d\n", num4);
return (0);
}
int _strcmp(char *s1, char *s2)
{
int i, len, len1 = 0, len2 = 0;
for (i = 0; s1[i] != '\0'; i++)
len1++;
for (i = 0; s2[i] != '\0'; i++)
len2++;
len = len1 > len2 ? len1 : len2;
for (i = 0; i < len; i++)
{
if (s1[i] != s2[i])
{
return (s1[i] - s2[i]);
break;
}
else if (s1[i] == s2[i])
{
if (s1[i + 1] == '\0' && s2[i + 1] != '\0')
{
return (-32);
}
if (s1[i + 1] != '\0' && s2[i + 1] == '\0')
{
return (32);
}
if (s1[i + 1] == '\0' && s2[i + 1] == '\0')
{
return (0);
}
}
}
}
If you compile and run this code, you should get the same result as below:
This is how to build the strcmp ( ) function from scratch, however, this is not the only method available, you can also come up with your method, given that you understand what is happening behind the function.
You can also man strcmp on Google to understand how the strcmp ( ) function works behind the scene.
Conclusion
This is how you can build your strcmp (string compare) string function from scratch and implement it.
Thank you for reading, you can connect with me on Twitter and LinkedIn.