Comparing Strings in C++: An Overview of 3 Methods
Discover in our latest blog post how to compare strings in C++ using three different methods. We’ll show you how to use strcmp(), the built-in compare() function, and relational operators (==, !=). Find the best approach for your use case!
1. Using the String strcmp() function in C++
The C++ String class provides built-in functions for manipulating strings. The strcmp() function is a C library function used to compare two strings in a lexicographical manner.
strcmp() Syntax
The input string must be a char array of C-style String. strcmp() compares the strings in a case-sensitive form.
int strcmp(const char *str1, const char *str2);
This function returns the following values according to the matching cases:
- Returns 0 if both the strings are the same.
- Returns < 0 (less than zero) if the value of the character of the first string is smaller as compared to the second string input.
- Results out to be > 0 (greater than zero) when the second string is greater in comparison.
Example 1: Using strcmp()
#include
#include
int main() {
const char *str_inp1 = "String Match";
const char *str_inp2 = "String Unmatch";
std::cout << "String 1: " << str_inp1 << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
if (strcmp(str_inp1, str_inp2) == 0)
std::cout << "\nBoth the input strings are equal." << std::endl;
else
std::cout << "\nThe input strings are not equal." << std::endl;
}
2. Using the compare() function in C++
C++ has a built-in compare() function to compare two strings.
compare() Syntax
int compare (const string& string-name) const;
This function returns the following values according to the matching cases:
- Returns 0 if both the strings are the same.
- Returns < 0 (less than zero) if the value of the character of the first string is smaller as compared to the second string input.
- Results out to be > 0 (greater than zero) when the second string is greater in comparison.
Example 1: Using compare()
#include
int main() {
std::string str_inp1("String Match");
std::string str_inp2("String Match");
std::cout << "String 1: " << str_inp1 << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
int res = str_inp1.compare(str_inp2);
if (res == 0)
std::cout << "\nBoth the input strings are equal." << std::endl;
else if (res < 0)
std::cout << "\nString 1 is smaller as compared to String 2." << std::endl;
else
std::cout << "\nString 1 is greater as compared to String 2." << std::endl;
}
3. Relational Operators in C++
C++ Relational operators such as == (double equals) and != (not equals) can be helpful in the comparison of strings.
Relational Operators Syntax
string1 == string2
string1 != string2
Example 1: Using C++ == operator
#include
int main() {
std::string str_inp1;
std::string str_inp2;
std::cout << "Enter the String 1:\n"; std::cin >> str_inp1;
std::cout << "Enter the String 2:\n"; std::cin >> str_inp2;
if (str_inp1 == str_inp2)
std::cout << "Strings are equal" << std::endl;
else
std::cout << "Strings are not equal" << std::endl;
}