Answer to Question #186099 in C++ for Ricky

Question #186099

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
Expert's answer
2021-04-27T05:23:35-0400
//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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS