Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Consider the following C++ code: string str1; string str2; char ch; int index

ID: 3841547 • Letter: 3

Question

3. Consider the following C++ code:

string str1;

string str2;

char ch;

int index;

cin >> str1;

cin >> str2;

cin >> index;

ch = str1[index];

str1[index] = str2[index];

str2[index] = ch;

cout << str1 << " " << str2 << endl;

Answer the following questions:

a. What is the output if the input is Hello There 2?

b. What is the output if the input is Diamond Gold 0?

c. What is the output if the input is C++ Java 1?

4. Suppose that you have the following statements:

string str1, str2;

cin >> str1 >> str2;

if (str1 == str2)

cout << str1 + '!' << endl;

else if (str1 > str2)

cout << str1 + " > " + str2 << endl;

else

cout << str1 + " < " + str2 << endl;

Answer the following questions:

a. What is the output if the input is Programming Project?

b. What is the output if the input is Summer Trip?

c. What is the output if the input is Winter Cold?

Explanation / Answer

3.

a.

Input : Hello There 2

cin >> str1; // str1 - Hello
cin >> str2; // str2 - There
cin >> index; // index - 2
ch = str1[index]; // ch = l
str1[index] = str2[index]; // now string 1 becomes Heelo
str2[index] = ch;   // now string 2 becomes Thlre
cout << str1 << " " << str2 << endl; // Heelo Thlre
Output :

Heelo Thlre

b.

Input : Diamond Gold 0

cin >> str1; // str1 - Diamond
cin >> str2; // str2 - Gold
cin >> index; // index - 0
ch = str1[index]; // ch = D
str1[index] = str2[index]; // now string 1 becomes Giamond
str2[index] = ch;   // now string 2 becomes Dold
cout << str1 << " " << str2 << endl; //Giamond Dold

Output :

Giamond Dold

c.

Input : C++ Java 1

cin >> str1; // str1 - C++
cin >> str2; // str2 - Java
cin >> index; // index - 1
ch = str1[index]; // ch = +
str1[index] = str2[index]; // now string 1 becomes Ca+
str2[index] = ch;   // now string 2 becomes J+va
cout << str1 << " " << str2 << endl; // Ca+ J+va
Output :

Ca+ J+va

4.

a.

Input : Programming Project

string str1, str2;

cin >> str1 >> str2; // str1 - Programming str2 - Project

if (str1 == str2)

cout << str1 + '!' << endl;

else if (str1 > str2)

cout << str1 + " > " + str2 << endl;

else // compare Programming and Project, where g < j condition satisfied

cout << str1 + " < " + str2 << endl; // Programming < Project

Output :

Programming < Project

b.

Input : Summer Trip

string str1, str2;

cin >> str1 >> str2; // str1 - Summer str2 - Trip

if (str1 == str2)

cout << str1 + '!' << endl;

else if (str1 > str2)

cout << str1 + " > " + str2 << endl;

else // compare Summer and Trip, where S < T condition satisfied

cout << str1 + " < " + str2 << endl; // Summer < Trip

Output :

Summer < Trip

c.

Input : Winter Cold

string str1, str2;

cin >> str1 >> str2; // str1 - Winter str2 - Cold

if (str1 == str2)

cout << str1 + '!' << endl;

else if (str1 > str2) // compare Winter and Cold, where W > C condition satisfied

cout << str1 + " > " + str2 << endl; // Winter > Cold

else   

cout << str1 + " < " + str2 << endl;

Output :

Winter > Cold

Index 0 1 2 3 4 str1 H e l l o str2 T h e r e
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote