Signup/Sign In
PUBLISHED ON: APRIL 4, 2022

C++ Program To Compare Two String

Here, in this tutorial we will discuss the different ways to compare the given strings in the C++ programming language. The comparison of the string determines whether the first string is equal to another string or not. Example: HELLO and Hello are two different strings.

Compare Two String In C++ Language

There are different ways to compare the strings in the C++ programming language, as follows:

  1. Using strcmp() function
  2. Using compare() function
  3. Using Relational Operator
  4. Using For loop and If statement
  5. Using user-defined function

Here, we will be discussing only the first method in this tutorial.

strcmp() function

The strcmp() is a pre-defined library function of the string.h header file. The strcmp() function compares two strings on a lexicographical basis. This means that the strcmp() function starts comparing the first string with the second string, character by character until all characters in both strings are the same or a NULL character is encountered.

Syntax

  1. int strcmp ( const char *leftstr, const char *rightstr );

Parameters:

leftstr: It defines the characters of the left string.

rightstr: It defines the characters of the right string.

Returns:

The leftstr string compares each character with the second string from the left side till the end of both strings. And, if both the strings are equal, the strcmp() function returns strings are equal. Else, the strings are not equal.

Let's create a program to compare strings using the strcmp() function in C++.

Program1.cpp

#include <iostream>  
using namespace std;  
#include <string.h>  
  
int main ()  
{
 // declare strings  
    const char *str1 = " Welcome to Studytonight";  
    const char *str2 = " Welcome to studytonight";  
      
    const char *str3 = " Studytonight";  
    const char *str4 = " Studytonight";  
      
    cout << " String 1: " << str1 << endl;  
    cout << " String 2: " << str2 << endl;  
      
    // use strcmp() function to validate the strings are equal.  
    if (strcmp (str1, str2) == 0)  
    {  
        cout << " \n Both strings are equal. " << endl;  
    }  
    else   
        {  
          
        cout << " The strings are not equal. " << endl;  
    }  
          
    cout << " \n String 3: " << str3 << endl;  
    cout << " String 4: " << str4 << endl;  
      
    // use strcmp() function to validate the strings are equal  
    if (strcmp (str3, str4) == 0)  
    {  
        cout << " \n Both strings are equal. " << endl;  
    }     
else   
        cout << " \n The strings are not equal. ";      
          
return 0;  
}  


String 1: Welcome to Studytonight
String 2: Welcome to Studytonight

Both strings are equal.

String 3: Studytonight
String 4: StudyTonight

The strings are not equal.

Conclusion

Here, in this tutorial, we have learned how to compare two strings.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.