Tutorial

3 Ways to Compare Strings in C++

Updated on April 22, 2024
authorauthor

Safa Mulani and Bradley Kouchi

3 Ways to Compare Strings in C++

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. String strcmp() function
  2. built-in compare() function
  3. C++ Relational Operators (==, !=)

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 char array 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 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.

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:

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:

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 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()

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 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:

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 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:
DigitalOcean
Enter the String 2:
digitalocean
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:
DigitalOcean
Enter the String 2:
DigitalOcean
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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Safa Mulani

author



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 7, 2021

This Was very educative

- Lyson Phiri

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 27, 2020

    nicely explained!!

    - manoj

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      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

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.