Tutorials  /  Linux Basics

Comparing Strings in C++: An Overview of 3 Methods

Ccentron Redaktion · May 2024 ·3 min read ·Linux Basics, Tutorial

Discover in our latest blog post how to compare strings in C++ using three different methods. We'll show you how to use strcmp(), the built-in compare() function, and relational operators (==, !=). Find the best approach for your use case!

1. Using the String strcmp() function in C++

The C++ String class provides built-in functions for manipulating strings. The strcmp() function is a C library function used to compare two strings in a lexicographical manner.

strcmp() Syntax

The input string must be a char array of C-style String. strcmp() compares the strings in a case-sensitive form.

Code
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.

Example 1: Using strcmp()

Code
#include 
#include 

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;
}
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

2. Using the compare() function in C++

C++ has a built-in compare() function to compare two strings.

compare() Syntax

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

Code
#include 

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;
}

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

Code
string1 == string2
Code
string1 != string2

Example 1: Using C++ == operator

Code
#include 

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;
}

Product Integration with ccloud³

If you want to run your C++ applications in a powerful, hosted environment, ccloud³ by centron offers the optimal solution:

  • Managed Cloud Hosting for C++ Applications – Scalable VMs for optimal performance
  • Optimized Development Environment – Perfect for C++ projects with Boost & standard libraries
  • Secure Infrastructure – Protected environments with reliable backups

Experience ccloud³ now! Learn more

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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren