In: Computer Science
Write a progrm that accepts two strings as input, then indicates if the two strings are equal when the case of individual letters is ignored.
Sample runs are shown below. Use C++
Enter two strings. String 1: PROgraM String 2: proGram PROgraM and proGram are equal.
Enter two strings. String 1: litter String 2: LittLe litter and LittLe are not equal.
C++ code:
#include <iostream>
using namespace std;
int main(){
//initializing 2 strings
string string1,string2;
//asking for them
cout<<"Enter two strings."<<endl;
//asking for String 1
cout<<"String 1: ";
//accepting it
cin>>string1;
//asking for String 2
cout<<"String 2: ";
//accepting it
cin>>string2;
//checking if their lengths are not equal
if(string1.length()!=string2.length())
//printing they are not equal
cout<<"\n"<<string1<<" and
"<<string2<<" are not equal."<<endl;
else{
//initializing flag as 0
int flag=0;
//looping till the end of the string
for(int i=0;i<string1.length();i++){
//converting current character of both strings to lower case and
checking if they are equal
if(tolower(string1[i])!=tolower(string2[i]))
//setting flag as 1
flag=1;
}
//checking if flag is 0
if(flag==0)
//printing they are equal
cout<<"\n"<<string1<<" and
"<<string2<<" are equal."<<endl;
else
//printing they are not equal
cout<<"\n"<<string1<<" and
"<<string2<<" are not equal."<<endl;
}
return 0;
}
Screenshot:
Input and Output: