0
0
CppHow-ToBeginner · 3 min read

How to Compare Strings in C++: Syntax and Examples

In C++, you can compare strings using the == operator for equality or the compare() method for detailed comparison. The == operator returns true if strings are equal, while compare() returns an integer indicating lexicographical order.
📐

Syntax

There are two common ways to compare strings in C++:

  • Using the == operator: Checks if two strings are exactly equal.
  • Using the compare() method: Returns 0 if strings are equal, a negative number if the first string is less, and a positive number if it is greater.
cpp
std::string str1 = "apple";
std::string str2 = "banana";

// Using == operator
bool areEqual = (str1 == str2);

// Using compare() method
int result = str1.compare(str2);
💻

Example

This example shows how to compare two strings using both == and compare(). It prints whether the strings are equal or which one comes first alphabetically.

cpp
#include <iostream>
#include <string>

int main() {
    std::string a = "cat";
    std::string b = "dog";

    if (a == b) {
        std::cout << "Strings are equal." << std::endl;
    } else {
        std::cout << "Strings are not equal." << std::endl;
    }

    int cmp = a.compare(b);
    if (cmp == 0) {
        std::cout << "Strings are equal (compare)." << std::endl;
    } else if (cmp < 0) {
        std::cout << "'" << a << "' comes before '" << b << "'." << std::endl;
    } else {
        std::cout << "'" << a << "' comes after '" << b << "'." << std::endl;
    }

    return 0;
}
Output
Strings are not equal. 'cat' comes before 'dog'.
⚠️

Common Pitfalls

Common mistakes when comparing strings in C++ include:

  • Using == to compare C-style strings (char*) instead of std::string, which compares pointers, not content.
  • Not considering case sensitivity; == and compare() are case-sensitive.
  • Using compare() without checking the sign of the result properly.

Always use std::string for safe and clear string comparisons.

cpp
#include <iostream>
#include <cstring>

int main() {
    const char* cstr1 = "hello";
    const char* cstr2 = "hello";

    // Wrong: compares pointers, not content
    if (cstr1 == cstr2) {
        std::cout << "C-style strings are equal (wrong)." << std::endl;
    } else {
        std::cout << "C-style strings are not equal (wrong)." << std::endl;
    }

    // Correct: use strcmp for C-style strings
    if (std::strcmp(cstr1, cstr2) == 0) {
        std::cout << "C-style strings are equal (correct)." << std::endl;
    }

    return 0;
}
Output
C-style strings are not equal (wrong). C-style strings are equal (correct).
📊

Quick Reference

MethodDescriptionReturn Value
operator==Checks if two std::string objects are equalbool (true or false)
compare()Lexicographically compares two stringsint (0 if equal, <0 if less, >0 if greater)
strcmp()Compares two C-style strings (char*)int (0 if equal, <0 if less, >0 if greater)

Key Takeaways

Use std::string and the == operator for simple equality checks.
Use std::string::compare() to find lexicographical order between strings.
Avoid using == with C-style strings; use strcmp() instead.
String comparisons in C++ are case-sensitive by default.
Check the sign of compare() result to determine string order.