Write a C++ program that will take in two strings str1[20] and str2[20] from user and do the following:
(a) Copy str1 to str2
(b) Compare str1 and str2, display 1 if str2 is greater and display 2 if str1 is greater.
//1
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1, str2;
cout<<"Enter a String: ";
cin>>str1;
cout<<"Enter another String: ";
cin>>str2;
str2 = str1;
cout<<str1;
cout<<str2;
return 0;
}
// 2
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1, str2;
cout<<"Enter a String: ";
cin>>str1;
cout<<"Enter another String: ";
cin>>str2;
if (str1.length() < str2.length())
{
cout<<"1";
}
else
{
cout<<"2";
}
return 0;
}
Comments
Leave a comment