By Safa Mulani and Bradley Kouchi

In this article, you will learn methods to compare strings in C++.
Strings in C++ can be compared using one of the following techniques:
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() Syntaxchar array of C-style String.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:
0 if both the strings are the same.< 0 (less than zero) if the value of the character of the first string is smaller as compared to the second string input.> 0 (greater than zero) when the second string is greater in comparison.strcmp() Example 1Run 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:
OutputString 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 2Run 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:
OutputString 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.
compare() function in C++C++ has a built-in compare() function to compare two strings.
compare() SyntaxThe compare() function compares two strings:
int compare (const string& string-name) const;
This function returns the following values according to the matching cases:
0 if both the strings are the same.< 0 (less than zero) if the value of the character of the first string is smaller as compared to the second string input.> 0 (greater than zero) when the second string is greater in comparison.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():
OutputString 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.
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:
OutputString 1: String Match
Strings are equal.
Then, str_inp0 is compared to str_inp2:
OutputString 2: String Unmatch
Strings are not equal.
This code directly compared a string with another input string to the compare() function.
C++ Relational operators such as == (double equals) and != (not equals) can be helpful in the comparison of strings.
Check if two values are equal:
string1 == string2
Check if two values are not equal:
string1 != string2
== operatorRun 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:
DigitalOcean
Enter the String 2:
digitalocean
Strings are not equal
The code will compare the two strings with ==.
!= operatorRun 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:
DigitalOcean
Enter the String 2:
DigitalOcean
Strings are equal
The code will compare the two strings with !=.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.