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

h09: Strings and Recursion

ready? assigned due points
true Tue 08/21 08:00AM Tue 08/28 11:00AM

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

MAY ONLY BE TURNED IN IN THE LECTURE 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 four lowest scores (if you have zeros, those are the four lowest scores).


Reading: Chapter 8.1, 8.2, 14

  1. (10 pts) Fill in the information in the header. The following are required to get the 10 "participation" points.
    • Filling in your name and umail address.
    • For paper submission PLEASE submit on ONE SHEET OF PAPER, double-sided if at all possible. If you must submit on two printed sheets write name on BOTH sheets and no staples, paperclips, or folded corners.
  2. (2 pts) How does a recursive function know when to stop recursing?
  3. (5 pts) What is stack overflow? How does it relate to recursion?
  4. (4 pts) How are ordinary arrays of characters and c-strings similar and how are they dissimilar?
  5. (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";
    
  6. (15 pts) Write a recursive function, int countVowels(string s) that returns the number of vowels in the string s.
  7. (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;