![]() |
VOOZH | about |
String comparison refers to the process of comparing two strings to check if they are equal or determine their lexicographical order. C provides the strcmp() library function to compare two strings but in this article, we will learn how to compare two strings without using strcmp() function.
The most straightforward method to compare two strings without using strcmp() function is by using a loop to compare the difference between ASCII values of each character in corresponding position. Let's take a look at example:
Identical strings
Explanation: This method iterates through both strings simultaneously, compares each character and returns the difference between the last compared characters. If the last compared character differs, it returns non-zero value. If they are same, we reached the null terminator '\0' in both strings, it returns 0.
There is one more method using which we can compare two strings:
The two strings can also be compared by using relational operators without finding the difference between the ASCII values.
Equal