#include<iostream.h>
#include<conio.h>
int compare(char ch1[],char ch2[])
{
                int i=0;
                while((ch1[i]==ch2[i]) && (ch1[i]!='\0'))
                                {
                                i++;
                                }
                if(ch1[i]==ch2[i])
                                {
                                return 0;
                                }
                else if(ch1[i]<ch2[i])
                                {
                                return (-1);
                                }
                else
                                {
                                return 1;
                                }
}
void main()
{
                char str1[10],str2[10];
                int result;
                clrscr();
                cout<<"Enter First String : ";
                cin>>str1;
                cout<<"Enter Second String : ";
                cin>>str2;
                result=compare(str1,str2);
                cout<<endl;
                if(result==0)
                                {
                                cout<<"Both Strings are Same";
                                }
                else if(result==-1)
                                {
                                cout<<str1<<" Is Smaller Than "<<str2;
                                }
                else
                                {
                                cout<<str1<<" Is Longer Than "<<str2;
                                }
                getch();
}

127.PNG

Advertisements