1
h10
CS16 S19
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h10: Chapter 8: Strings

ready? assigned due points
true Fri 05/24 09:00AM Fri 06/07 11:59PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GAUCHOSPACE. There is NO MAKEUP for missed assignments.


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

Read Chapter 8 and the lecture notes. You don’t need to turn this homework in. To earn credit for this homework, complete the corresponding quiz on gauchospace AFTER you have completed the pen and pencil version of the homework. The quiz will be available one day before the due date indicated on the homework.

PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL! FOR BEST RESULTS, SAVE THIS PAGE AS A PDF, THEN PRINT THE PDF.

1.(2 pts) How are ordinary arrays of characters and c-strings similar and how are they dissimilar?

2.(4 pts) What are two (2) things that are wrong with this use of a c-string (ignore why someone would write this code, focus on logic/syntax errors)?

char s1[5] = "Mark", s2[5] = "Jill";
for (int i = 0; i <= 5; i++)
    s1[i] = s2[i];
if (s1 != s2) s1 = "Art";

3.(4 pts) What is the output of the following code?

char s1[4] = "abc", s2[4] = "abc";
if (s1==s2) cout << "Strings are equal";
else cout << "Strings are not equal";

4.(10 pts) The following code takes in a string input from the user and performs an integer multiplication, as seen in the example run here. Note that the input string will contain the asterix character (i.e. *):

Enter 2 integer numbers to be multiplied, like this: num1*num2: 15*3
The answer is: 45

Complete the missing code below that performs this task (it can be done in 2 lines, but you can use more if you like).

string s; int k(0);
cout << "Enter 2 integer numbers to be multiplied, like this: num1*num2: ";
cin >> s;




cout << "The answer is: " << k << endl;

5.(10 pts) Show the output produced when the following code (entire program not shown) is executed. You are encouraged to also try to compile this in a program to verify your results.

    string name = "Jeffery Tambor";

    cout << "NAME = " + name << endl;
    cout << name.length() << endl;

    name.erase(8, 6);
    cout << name << endl;
    name.append("Dean WD Morgan");
    cout << name << endl;

    name.insert(22, "@TWD");
    cout << name << endl;
    name.replace(23, 3, "The WD");
    cout << name << endl;

    cout << name.find("WD") << endl;
    cout << name.rfind("WD") << endl;
    cout << name.rfind("fery") << endl;

    for (int i = name.length(); i > 20; i--)
        cout << name[i-1];
    cout << endl;