Comparing Two Strings in C++
String comparison is a very common scenario in programming. Most string comparison algorithms compare the strings character by character. C++ provides us with built-in operators like == and != to compare the strings along with strcmp and compare functions. In this article, we will delve into three approaches for comparing strings in C++:
- Comparing two strings in C++ Using strcmp() Function in C++
- Comparing Two Strings in C++ Using compare() Function
- Comparing Two Strings in C++ Using C++ Relational Operators
1. Comparing two strings in C++ Using strcmp() Function
strcmp() is a C library function that lexicographically compares two strings.
The strcmp() function takes two strings (character arrays) as arguments and returns:
- 0 if both strings are lexicographically equal.
- Greater than 0 if firstString is lexicographically greater than secondString.
- Less than 0 if firstString is lexicographically smaller than secondString.
In lexicographical order, if a word in a dictionary comes before another, the first word is considered lexicographically smaller. If they appear together in a dictionary, they are lexicographically equal.
For more information, read: strcmp() in C++
2. Comparing Two Strings in C++ Using compare() Function
compare() is a function defined in the standard library of C++ to compare two strings.
compare() function in C++ gives broadly two classes of outputs when comparing strings:
A. Class #1: equal to 0 ( == 0 )
Zero is returned when the strings being compared are lexicographically equal.
B. Class #2: not equal to 0 ( != 0 )
compare() function in C++ returns non-zero values when the strings being compared are lexicographically unequal
There are further two sub-classes of outputs for non-zero output returned by the compare() function:
- Greater than 0 ( > 0): this is returned when the first string is lexicographically greater than the second string
- Less than 0 ( < 0 ): this is returned when the first string is lexicographically smaller than the second string
Let’s understand this better using a code example in C++:
3. Comparing Two Strings in C++ Using C++ Relational Operators
Apart from the inbuilt functions of strcmp() & compare(), C++ also offers a vanilla way of comparing two strings using relational operators ( ==, != )
You might have observed that we used the ‘==’ relational operator many times above.
E.g., when we use equalOrNot == 0 in our code examples above, we are simply comparing whether the value of our equalOrNot variable is equal to 0.
Therefore, the ‘==’ operator is used to compare two entities (strings or integers, for instance) to see whether they are equal in value. It’s undoubtedly one of the easiest ways to compare two strings in C++. There are two kinds of relational operators we wish to highlight here:
A. ‘==’ relational operator or “equals to” relational operator
B. ‘!=’ relational operator or “not equals to” relational operator
Let’s further understand comparing strings using these relational operators via a code example in C++:
A. Using ‘==’ (equals to) relational operator in C++:
B. Using ‘!=’ (not equals to) relational operator in C++
Note how even if the way of writing the conditions in code changes, the outputs remain unchanged in the above programs (as “Scaler” and “Scaled” strings are indeed unequal).
For more information, read: What are Relational Operators in C++?
In this article, we learned three ways to compare strings in C++, using:
- strcmp() inbuilt function
- compare() inbuilt function, and
- C++ relational operators ( ‘==’, ‘!=’)