C S C O R N E R

Comparing Two Strings in C++

//Using Dev C++ Compiler
#include<iostream>
#include<string>
#include<cstring>
#include<conio.h>
 
using namespace std;
 
main()
{
 string input1, input2;
 int len1, len2;
  
 //getting strings as input from user
  
 cout<<"Enter String 1 : ";
 cin>>input1;
 cout<<"Enter String 2 : ";
 cin>>input2;
 cout<<"\n\n";
  
 //getting lenght of strings
  
 len1=input1.size();
 cout<<"length of string 1 is : "<<len1<<endl; 
  
 len2=input2.size();
 cout<<"length of string 2 is : "<<len2<<endl;
 cout<<"\n\n";
  
//converting string 1's each character in upercase
 for(int i = 0; i < input1.size(); i++)
 
 input1[i] = toupper(input1[i]);
//printing string 1 in upper case
{
 cout<<"String 1 in upper case : "<<input1<<endl; 
 cout<<"\n\n"; 
}
//converting string 2's each character in upercase
 for(int i = 0; i < input2.size(); i++)
 input2[i] = toupper(input2[i]);
//printing string 2 in upper case
{
 cout<<"String 2 in upper case : "<<input2<<endl;
 cout<<"\n\n"; 
}
//comparing strings to find, string are similar of different, its for if strings are different
 if(input1.compare(input2) !=0)
{
 cout<<"Both String are different"<<endl;
 cout<<"\n\n";
//converting again in lower cas but not fist character of strings
 
 for(int i = 1; i < input1.size(); i++)
 input1[i] = tolower(input1[i]);
  
 for(int i = 1; i < input2.size(); i++)
 input2[i] = tolower(input2[i]);
  
 //concating and printing the strings 
  
cout<<"Both strings after concatenanton : "<<input1<<input2<<endl;
}
 
// printing strings if strings are similar
else
{
if(input1.compare(input2) ==0)
 
cout<<"Both String are same"<<endl;
 
for(int i = 1; i < input1.size(); i++)
 input1[i] = tolower(input1[i]);
  
for(int i =1 ; i < input2.size(); i++)
input2[i] = tolower(input2[i]);
 
cout<<"Both strings after concatenanton : "<<input1<<input2<<endl;
}
getch(); 
}

No comments:

Powered by Blogger.