1
h10
CS16 F18
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 Thu 11/29 12:30PM Wed 12/05 12:30PM

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 GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


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. Please turn in the physical copy of your homework in person. Write in PEN or DARK PENCIL.

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;