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

7-9. Consider the following C++ code: string str1; string str2; char ch; int ind

ID: 3866809 • Letter: 7

Question

7-9. 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;

7. What is the output if the input is Hello here 0?


8. What is the output if the input is Diamond Art 1?


9. What is the output if the input is C++ python 2?


10-12. Suppose that you have the following statements:
string str4, str3;
cin >> str4 >> str3;
if (str3 == str4)
    cout << str3 + " = " + str4 << endl;
else if (str3 > str4)
    cout << str3 + " > " + str4 << endl;
else
    cout << str3 + " < " + str4 << endl;
Answer the following questions:


10. What is the output if the input is card cart?


11. What is the output if the input is cart car?


12. What is the output if the input is Car Car?


13-16. What is the output of the following statements? (Assume that all parts are independent of each other.)

13. string str = "*Happy new years!*";
   cout << str.substr(8, 5) << endl;

14. string str = "*Happy new years!*";
   string::size_type ind = str.find('y');
   string s = str.substr(ind, 4);
   cout << s << endl;


15. string str = "*Happy new years!*";
   cout << str.insert(6, ", new") << endl;

16.  string str = "*Happy new years!*";
  str.insert(0, "race car");

  str.erase(8, 15);

  cout << str << endl;

  cout << endl;

Explanation / Answer

(7-9) C++ Code:

   Given code swaps the index of the character in the strings entered.

  
(7)
   Input: Hello here 0
  
   Swaps Characters H with h and vice versa
  
   Hence Output is: hello Here
  
  
(8)  
   Input: Diamond Art 1
  
   Swaps Characters i with r and vice versa
  
   Hence Output is: Dramond Ait
  
  
(9)  
   Input: C++ python 2
  
   Swaps Characters + (Second) with t and vice versa
  
   Hence Output is: C+t py+hon

________________________________________________________________________________________
________________________________________________________________________________________
  
(10 - 12) Comparing strings
  
(10)
       Input:    str3 = cart       str4 = card
      
       As str3 is greater than str4
      
       Output: cart > card
      

(11)  
       Input:    str3 = cart       str4 = car
      
       As str3 is less than str4
      
       Output: car < cart
      
      
(!2)  
      
       Input:    str3 = Car       str4 = Car
      
       As str3 and str4 are same
      
       Output: Car = Car
      
________________________________________________________________________________________
________________________________________________________________________________________

(13)
   Value in the string variable str is *Happy new years!*
  
   substr function extracts a substring from the actual string.
  
   Syntax: substr(x, y)
  
   x - Starting index
   y - number of characters starting from index x
  
   str.substr(8, 5)
  
   Character at index 8 is e. Starting from character 'e' extracts five characters.
  
   Hence output will be: ew ye

________________________________________________________________________________________

(14)
   Value in the string variable str is *Happy new years!*
  
   find function returns the first found index in the stirng
  
   substr function extracts a substring from the actual string.
  
   Syntax: substr(x, y)
  
   x - Starting index
   y - number of characters starting from index x
  
   str.find('y') returns value 5
  
   str.substr(ind, 4) changes to str.substr(5, 4)
  
   Character at index 5 is y. Starting from character 'y' extracts four characters.
  
   Hence output will be: y ne
  
________________________________________________________________________________________

(15)
   Value in the string variable str is *Happy new years!*
  
   insert function inserts a string at the specified index.
  
   str.insert(6, ", new") returns string: *Happy, new new years!*
  
   Hence output will be: *Happy, new new years!*
  
________________________________________________________________________________________

(16)
   Value in the string variable str is *Happy new years!*
  
   insert function inserts a string at the specified index.
  
   str.insert(0, "race car"); returns string: race car*Happy, new new years!*
  
   erase function removes specified number of characters starting from specified index.
  
   Syntax: erase(x, y)
  
   x - Starting index
   y - number of characters starting from index x
  
  
   str.erase(8, 15); returns string: race cars!*
  
  
   Hence output will be: race cars!*
  
       

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