Support Online
Skip to main content

C++ String Comparison

What Will You Learn in This Guide?

In this guide, you will learn how to compare the equality or dictionary order of two texts in C++.
We will learn 3 basic methods:

  • Relational operators (==, !=)
  • std::string::compare() method
  • strcmp() function (for C-style arrays)

With code examples, we explain step by step which method to use and when.

The simplest and most readable way to compare std::string objects in C++ is with relational operators.
These operators are overloaded for the std::string class and can be used directly.

Steps:

  1. Define the two std::string variables you want to compare.
  2. Check equality with operator == and check difference with operator !=.
  3. Comparison is case-sensitive.
#include <iostream>
#include <string>
using namespace std;

int main() {
string metin1 = "GirisToken123";
string metin2 = "GirisToken123";
string metin3 = "girisToken123"; // Küçük harfle başlıyor

if (metin1 == metin2)
cout << "Metin 1 ve Metin 2 eşit." << endl;
else
cout << "Metinler eşit değil." << endl;

if (metin1 != metin3)
cout << "Metin 1 ve Metin 3 farklı." << endl;

return 0;
}
  • This method is the most frequently preferred and highly readable comparison method in modern C++.

std::string::compare() Method — Detailed Dictionary Comparison

The std::string::compare() method determines not only equality but also the lexical order of texts.

Steps:

Call the compare() method on the first text.

Give the second text as parameter.

Check the return value.

Return Value Meaning 0 Texts are exactly the same < 0 The first text comes first in the dictionary

0 First text comes after in dictionary


#include <iostream>
#include <string>
using namespace std;

int main() {
string sunucu_adi1("tr-sunucu-a");
string sunucu_adi2("tr-sunucu-b");

int sonuc = sunucu_adi1.compare(sunucu_adi2);

if (sonuc == 0)
cout << "Metinler eşit." << endl;
else if (sonuc < 0)
cout << "Metin 1, Metin 2'den önce geliyor." << endl;
else
cout << "Metin 1, Metin 2'den sonra geliyor." << endl;

return 0;
}
  • The compare() method is especially preferred in sorting algorithms.

strcmp() Function — for C-Style Arrays

strcmp() is a classic comparison function inherited from the C language. It only works with C-style character arrays (char*, char[]).

Steps:

Define two C-style texts.

Call the strcmp(str1, str2) function.

Check the return value (0, <0, >0).


#include <iostream>
#include <cstring> // strcmp için
using namespace std;

int main() {
const char *token1 = "ornek.com";
const char *token2 = "ornek.com";

if (strcmp(token1, token2) == 0)
cout << "C-stil metinler eşit." << endl;
else
cout << "C-stil metinler eşit değil." << endl;

return 0;
}

strcmp() only works with char* arrays, not std::string objects.

Frequently Asked Questions (FAQ)

  1. Is the == operator or the compare() method faster?

There is no difference in modern compilers. == is recommended as it is more readable.

  1. How to do case insensitive comparison?

You can convert both texts to lowercase with std::tolower() and then compare.

  1. Why can't strcmp() be used with std::string?

Because strcmp() works with raw memory addresses. std::string is an abstract class.

  1. Is it possible to compare only a certain part?

Yes. A parameterized version of compare() or strncmp() can be used.


Result

When comparing text in C++, you should choose the method according to the data type you use:

  • for std::string: == or compare()

  • For C-style arrays: strcmp()

In terms of code readability, the == operator is the simplest and safest option. You can practice by testing these examples immediately on the GenixNode platform.