Table of Contents
Introduction
In this article, you will learn methods to compare strings in C++.
Strings in C++ can be compared using one of the following techniques:
1. Using the String strcmp() function in C++
C++ String has built-in functions for manipulating data of String type. The strcmp() function is a C library function used to compare two strings in a _lexicographical manner_.
strcmp() Syntax
- The input string has to be a
chararray of C-style String. - The
strcmp()compares the strings in a case-sensitive form as well.
int strcmp(const char <^>*str1<^>, const char <^>*str2<^>);
This function returns the following values according to the matching cases:
- Returns
0if 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.
strcmp() Example 1
Run the following code:
#include <iostream>
#include <string.h>
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;
}
This will generate the following output:
[secondary_label Output]
String 1: String Match
String 2: String Unmatch
The input strings are not equal.
strcmp(str_inp1, str_inp2) results in -9. The values of str_inp1 and str_inp2 are different.
strcmp() Example 2
Run the following code:
#include <iostream>
#include <string.h>
int main()
{
const char *str_inp1 = "String Match";
const char *str_inp2 = "String Match";
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;
}
This will generate the following output:
[secondary_label Output]
String 1: String Match
String 2: String Match
Both the input strings are equal.
strcmp(str_inp1, str_inp2) results in 0. The values of str_inp1 and str_inp2 are the same.
2. Using the compare() function in C++
C++ has a built-in compare() function to compare two strings.
compare() Syntax
The compare() function compares two strings:
int compare (const string& <^>string-name<^>) const;
This function returns the following values according to the matching cases:
- Returns
0if 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()
Run the following code:
#include <iostream>
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;
}
In this example, str_inp1 and str_inp2 are compared with compare():
[secondary_label Output]
String 1: String Match
String 2: String Match
Both the input strings are equal.
Both the strings are the same lexicographically, so the function returns 0.
Example 2: Using compare()
Run the following code:
#include <iostream>
int main()
{
std::string str_inp0("String Match");
std::string str_inp1("String Match");
std::string str_inp2("String Unmatch");
std::cout << "String 1: " << str_inp1 << std::endl;
if (str_inp1.compare(str_inp0) == 0)
std::cout << "\nStrings are equal." << std::endl;
else
std::cout << "\nStrings are not equal." << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
if (str_inp2.compare(str_inp0) == 0)
std::cout << "\nStrings are equal." << std::endl;
else
std::cout << "\nStrings are not equal." << std::endl;
}
In this example, str_inp0 is compared to str_inp1:
[secondary_label Output]
String 1: String Match
Strings are equal.
Then, str_inp0 is compared to str_inp2:
[secondary_label Output]
String 2: String Unmatch
Strings are not equal.
This code directly compared a string with another input string to the compare() function.
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
Check if two values are equal:
string1 == string2
Check if two values are not equal:
string1 != string2
Example 1: Using C++ == operator
Run the following code:
#include <iostream>
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;
}
Provide values for "String 1" and "String 2":
Enter the String 1:
the cloud provider
Enter the String 2:
the cloud provider
Strings are not equal
The code will compare the two strings with ==.
Example 2: Using C++ != operator
Run the following code:
#include <iostream>
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 not equal" << std::endl;
else
std::cout << "Strings are equal" << std::endl;
}
Provide values for "String 1" and "String 2":
Enter the String 1:
the cloud provider
Enter the String 2:
the cloud provider
Strings are equal
The code will compare the two strings with !=.
Conclusion
In this article, you learned methods to compare strings in C++. This included String's strcmp() function, the built-in compare() function, and relational operators (==, !=).
Continue your learning with more C++ tutorials.