codescracker
c++

C++ Program to Compare Two Strings



« Previous Program Next Program »


In this article, you will learn and get code on comparing of two strings in C++. The program is created in following ways:

Compare Two Strings without strcmp()

To compare two strings in C++ programming, you have to ask from user to enter the two strings and compare them without using any type of library function like strcmp() as shown in the program given below. Let's have a look at the program first, will get the explanation later on:

// C++ Program to Compare Two Strings without strcmp()
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    char str1[50], str2[50];
    int i=0, chk=0;
    cout<<"Enter the First String: ";
    cin>>str1;
    cout<<"Enter the Second String: ";
    cin>>str2;
    while(str1[i]!='\0' || str2[i]!='\0')
    {
        if(str1[i]!=str2[i])
        {
            chk = 1;
            break;
        }
        i++;
    }
    if(chk==0)
        cout<<"\nStrings are Equal";
    else
        cout<<"\nStrings are not Equal";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program compare strings

Now enter the value of first string say codescracker and then again enter the value of second string say codescracker. Press ENTER key to see the output as shown in the snapshot given below:

C++ program compare two strings

Here is another sample run, with user input codescracker as first string and crackercodes as second string:

compare two strings without strcmp c++

And here is the last sample run with user input code as first string and codes as second input:

compare two strings in c++

When user enters first string, then it gets initialized to str1 in a way that (supposing user enters code as first string):

In similar way, the second string gets initialized to str2. Now with user input, code and codes as first and second string, the dry run of above program goes like:

What if User enters Strings of Unequal length ?

As you can see the output with two strings code and codes, the while loop runs 4 times (comparing code, first 4 character of first string, with code, first 4 character of second string) without any reason. Because if the length of two strings are not equal, then it is surely that strings can not be same.

So we do not need to evaluate while loop in that case. Therefore the program given below first finds the length of string and then checks whether length of both strings are equal or not. If it is equal, then will proceed to compare the strings. Otherwise print a message like unequal length or anything else (depends on you):

// Compare Two Strings without using any Library Function
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    char str1[50], str2[50];
    int i, chk=0, len1=0, len2=0;
    cout<<"Enter the First String: ";
    cin>>str1;
    cout<<"Enter the Second String: ";
    cin>>str2;
    i=0;
    while(str1[i]!='\0')
    {
        len1++;
        i++;
    }
    i=0;
    while(str2[i]!='\0')
    {
        len2++;
        i++;
    }
    if(len1==len2)
    {
        i=0;
        while(str1[i]!='\0' || str2[i]!='\0')
        {
            if(str1[i]!=str2[i])
            {
                chk = 1;
                break;
            }
            i++;
      }
      if(chk==0)
        cout<<"\nStrings are Equal";
      else
        cout<<"\nStrings are not Equal";
    }
    else
        cout<<"\nString Length must has to be same, to Compare!";
    cout<<endl;
    return 0;
}

Here is its sample run with user input code and codes as first and second string:

compare two string c++

Compare Two Strings using strcmp()

With the help of library functions, the program becomes smaller. Because we do not need to write some extra code like finding length of string or comparing string, that can easily be processed with the help of library function like strlen() or strcmp().

The function, strcmp() takes two string as argument and returns 0 if both strings are equal. And the function strlen() takes single string as argument and returns its length.

// Compare Two Strings using strcmp() function
// ----codescracker.com----

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

Here is its sample run with user input codescracker as first and second string both:

compare two string using strcmp c++

Same Program in Other Languages


« Previous Program Next Program »