C++ Program to to compare two strings


January 31, 2023, Learn eTutorial
1246

Here is a C++ program to compare two strings and check whether the two strings are the same.

How to compare two strings?

Comparing two strings means comparing the characters of the string to check whether the strings are the same or not. Example string 1- learnetutorials, string 2- elearntutorials; here-string 1 and string 2 are not the same.

For comparing two strings, first we have to check the length of the strings. If the length of two strings is not equal, then it is sure that the string cannot be the same. So, while comparing two strings we have to compare the length of the strings and then the characters of the strings.

Here in this C++ program, we are going to check the length of the two strings and then compare the characters with the help of library functions.

  • strlen() is used to find the length of the strings
  • strcmp() to compare strings.

How to write a C++ program to compare and check two strings?

The user is asked to enter two strings. Read the strings into two different character arrays str1 [50], and str2 [50]; Use the library function strlen() to find the length of the two strings and get the length of the two strings to the integer type variables len1 and len2. Check if the length of the strings is equal. If they are not equal then print strings are not equal. If the lengths are equal then check the characters of the string by using the function strcmp(). If the function returns zero, then print the strings are equal, else not.

Algorithm

Step 1: Call the header file iostream.

Step 2:Call the header file string.h

Step 3: Use the namespace std.

Step 4: Open the integer type main function; int main().

Step 5: Declare two character type arrays str1[50],str2[50], integer type variables len1 and len2;

Step 6: Ask the user to enter the first string and read the string into the array str1[];

Step 7: Ask the user to enter the second string and read the string into the array str2[];

Step 8: Get the length of the first string to the variable len1;

Step 9: Get the length of the second string to the variable len2;

Step 10: Compare the length of the strings;

Step 11: If the length is not equal then print the strings are not equal;

Step 12: If the length is equal then compare the characters of the string;

Step 13: If the characters are the same then print the strings are equal.

Step 14: else print the strings are not equal;

Step 15: Exit


strlen()

The strlen() function in C++ returns the number of characters in the string which is its length. It is defined in the header file cstring. The syntax is:

strlen(const char* str)

where,

  • str is the string whose length we need to find out, which is cast to a const char*.

strcmp() in C++

The strcmp() function compares two null-terminating strings (C-strings). The comparison is done lexicographically. It is defined in header file cstring. The syntax is:

strcmp(const char* lhs, const char* rhs);

Here,

  • LHS stands for the left-hand side
  • RHS stands for the right-hand side

The strcmp() function returns a positive value if the first differing character in LHS is greater than the corresponding character in RHS, and a negative value if the first differing character in LHS is less than the corresponding character in RHS, and 0 if LHS and RHS are equal.

C++ Source Code

                                          #include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str1[50], str2[50];
    int len1, len2;
    cout<<"Enter the First String: ";
    cin>>str1;
    cout<<"Enter the Second String: ";
    cin>>str2;
    len1 = strlen(str1);
    len2 = strlen(str2);
    if(len1==len2)
    {
      if(strcmp(str1, str2)==0)
        cout<<"\nStrings are Equal";
      else
        cout<<"\nStrings are not Equal";
    }
    else
        cout<<"\nStrings are not Equal";
    cout<<endl;
    return 0;
}
                                      

OUTPUT

Run 1 
Enter the First String: learnetutorials
Enter the Second String: learnetutorials
Strings are Equal
Run 2
Enter the First String: learnetutorials
Enter the Second String: elearntutorials
Strings are not Equal
Run 3
Enter the First String: learnetutorials
Enter the Second String: learnetutorialslearnetutorials
Strings are not Equal