Q-24: Write a C/C++ program to compare two given strings.
    

Using - C


#include<stdio.h> #include<string.h> void main() { char str1[1024], str2[1024]; printf("Enter String-1: "); gets(str1); printf("Enter String-2: "); gets(str2); if(strcmp(str1,str2) == 0) { printf("Strings are equal!"); } else if(strcmp(str1,str2) < 0) { printf("First non-matching character in string-1 is lower (in ASCII) than that of string-2 !"); } else { printf("First non-matching character in string-1 is greater (in ASCII) than that of string-2 !"); } }

OUTPUT

Enter String-1:  Hello
Enter String-2:  hello
First non-matching character in string-1 is lower (in ASCII) than that of string-2 !

        

Using - C++


#include<iostream> #include<string.h> using namespace std; int main() { string str1, str2; cout<< "Enter String-1: "; getline(cin,str1); cout<< "Enter String-2: "; getline(cin,str2); if(str1.compare(str2) == 0) { cout<< "Strings are equal!"; } else if(str1.compare(str2) < 0) { cout<< "First non-matching character in string-1 is lower (in ASCII) than that of string-2 !"; } else { cout<< "First non-matching character in string-1 is greater (in ASCII) than that of string-2 !"; } return 0; }

OUTPUT

Enter String-1:  welcome
Enter String-2:  welcome
Strings are equal!