Wednesday, 2 July 2014

C++ program to compare two strings

Here are C++ programs to compare to string using strcmp functions, without using  strcmp()  function,using pointers, using user defined functions, using recursion and using operator overloading.


C++ program to find whether two strings are equal or not


#include<iostream>
using namespace std;
int main()
{
    char str1[20],str2[20];
    cout<<"Enter two String : ";
    cin>>str1>>str2;
    for(int i=0;str1[i]!='\0'||str2[i]!='\0';i++)
        if(str1[i]!=str2[i])
        {
           cout<<"Both strings are not equal ";
           return 0;
        }
    cout<<"Both strings are equal";
    return 0;
}



C++ program to compare two string without using  strcmp() function

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    char str1[20],str2[20];
    int i=0;
    cout<<"Enter two String : ";
    cin>>str1>>str2;
    while(1)
    {
        if(str1[i]>str2[i])
        {
            cout<<str1<<" is greater than "<<str2;
            break;
        }
        else if(str1[i]<str2[i])
        {
            cout<<str2<<" is greater than "<<str1;
            break;
        }
        else if(str1[i]==0&&str2[i]==0)
        {
            cout<<"Both strings are equal";
            break;
        }
        i++;
    }
    return 0;
}



C++ program to compare two string using strcmp() function

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
    char str1[20],str2[20];
    int res;
    cout<<"Enter two String : ";
    cin>>str1>>str2;
    res=strcmp(str1,str2);
    if(res<0)
       cout<<str2<<" is greater than "<<str1;
    else if(res==0)
       cout<<"Both strings are equal ";
    else
       cout<<str1<<" is greater than "<<str2;
    return 0;
}



C++ program to compare two string using pointers

#include<iostream>
using namespace std;
int main()
{
    char str1[20],str2[20],*p,*q;
    cout<<"Enter string a1: ";
    cin>>str1;
    cout<<"Enter string a2: ";
    cin>>str2;
    p=str1;
    q=str2;
    while(*p!='\0'||*q!='\0')
    {
        if(*p!=*q)
        {
            cout<<"Strings are not equal";
            return 0;
        }
        p++,q++;
    }
    cout<<"Strings are equal";
    return 0;
}



C++ program to compare two string using user define function

#include<iostream>
#include<conio.h>
using namespace std;
int cmpstr(char *str1,char *str2)
{
    int i=0;
    while(1)
    {
        if(str1[i]>str2[i])
            return 1;
        else if(str1[i]<str2[i])
            return -1;
        else if(str1[i]==0&&str2[i]==0)
            return 0;
        i++;
    }
}
int main()
{
    char str1[20],str2[20];
    int res;
    cout<<"Enter two String : ";
    cin>>str1>>str2;
    res=cmpstr(str1,str2);
    if(res<0)
       cout<<str2<<" is greater than "<<str1;
    else if(res==0)
       cout<<"Both strings are equal ";
    else
       cout<<str1<<" is greater than "<<str2;
    return 0;
}




C++ program to compare two string using recursion

#include<iostream>
#include<conio.h>
using namespace std;
int cmpstr(char *str1,char *str2,int i)
{
    if(str1[i]>str2[i])
        return 1;
    else if(str1[i]<str2[i])
        return -1;
    else if(str1[i]==0&&str2[i]==0)
        return 0;
    cmpstr(str1,str2,i+1);
}
int main()
{
    char str1[20],str2[20];
    int res;
    cout<<"Enter two String : ";
    cin>>str1>>str2;
    res=cmpstr(str1,str2,0);
    if(res<0)
       cout<<str2<<" is greater than "<<str1;
    else if(res==0)
       cout<<"Both strings are equal ";
    else
       cout<<str1<<" is greater than "<<str2;
    return 0;
}



C++ program to find whether two strings are equal or not using operator overloading

#include<iostream>
using namespace std;
int main()
{
    char str1[20],str2[20];
    cout<<"Enter two String : ";
    cin>>str1>>str2;
    for(int i=0;str1[i]!='\0'||str2[i]!='\0';i++)
        if(str1[i]!=str2[i])
        {
           cout<<"Both strings are not equal ";
           return 0;
        }
    cout<<"Both strings are equal";
    return 0;
}



No comments:

Post a Comment