Introduction
In this article, you will learn how to compare strings in C++.
1. Using the strcmp() function in C++
C++ String has built-in functions for manipulating String data. The strcmp() function is a C library function used to compare two strings lexicographically.
- The input string must be a character array of C-style strings.
- strcmp() also compares strings in a case-sensitive form.
int strcmp(const char *str1, const char *str2);This function returns the following values depending on the matching items:
- Returns 0 if both strings are the same.
- If the character value of the first string is smaller compared to the second string input, it returns < 0 (less than zero).
- When the second string in the comparison is larger, the results are > 0 (greater than zero).
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 produces 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.
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 produces the following output:
Output
String 1: String Match
String 2: String Match
Both the input strings are equal.strcmp(str_inp1, str_inp2) returns 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.
The compare() function compares two strings:
int compare (const string& string-name) const;This function returns the following values depending on the matching items:
- Returns 0 if both strings are the same.
- If the character value of the first string is smaller compared to the second string input, it returns < 0 (less than zero).
- When the second string in the comparison is larger, the results are > 0 (greater than zero).
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():
Output
String 1: String Match
String 2: String Match
Both the input strings are equal.Both strings are lexically identical, so the function returns 0.
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:
Output
String 1: String Match
Strings are equal.Then str_inp0 is compared to str_inp2:
Output
String 2: String Unmatch
Strings are not equal.This code directly compares a string to another input string with the compare() function.
3. Relational operators in C++
C++ relational operators such as == (equals) and != (not equal) can be useful in comparing strings.
Check if two values are equal:
string1 == string2Check if two values are not equal:
string1 != string2Example 1: Using the 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 the values "String 1" and "String 2":
Enter the String 1:
DigitalOcean
Enter the String 2:
digitalocean
Strings are not equalThe code compares two strings with ==.
Example 2: Using the 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 the values "String 1" and "String 2":
Enter the String 1:
DigitalOcean
Enter the String 2:
DigitalOcean
Strings are equalThe code compares two strings with !=.
Result
In this article, you learned about string comparison methods in C++. This included the String strcmp() function, the built-in compare() function, and the relational operators (==, !=).









