Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [20pts] Assume the following delcarations and function definitions fill in the table. Note: "error" is a possiblity! Note: each question is independent, i.e. no side effects of one expression should be taken to carry over to the next
    string s = "happy";
    string t = "sad";
    string foo()
    {
      return "good";
    }
    char bar(string str)
    {
      int i = str.length()-1;
      return str[i];
    }
    bool mkcap(char& c)
    {
      if ('a' <= c && c <= 'z') 
      {
        c = c - 'a' + 'A';
        return true; 
      }
      return false;
    }
    
    expressiontype (or error!)value (or error!)
    s[1]
    t[2] - 'a' + 'A'
    foo() + 2.5
    s[0] = t[0]
    bar(foo())
    foo()[3]
    bar(s[0])
    mkcap(s)
    mkcap(s[0])
    mkcap(s[0] + t[0])
  2. [80Pts] Write a program (call it hw20.cpp) that reads a target string A and a test string S and tells the user whether A is a substring of S. A is a substring of S means that A appears (in order and together!) in S somwhere. For example: low is a substring of allowable (see it? "allowable"), but not of withhold (see the letters are there but not in order in "withhold"), nor in landowner (see the letters are there and in order but not together in "landowner"). A typical run of your program should look like this:
    ~/$ ./prog
    Enter target string: act
    Enter test string  : reactionary
    The target act is a substring of the test string reactionary!
    ~/$ ./prog
    Enter target string: ar
    Enter test string  : reactionary
    The target ar is a substring of the test string reactionary!
    ~/$ ./prog
    Enter target string: ryes
    Enter test string  : reactionary
    The target ryes is not substring of the test string reactionary!

    Important Hint: It's not a bad idea to start off by trying to determine if a target string A matches a test string S starting at index i of S. So that, for example, if A="the" and S="other" you wouldn't find a match starting at index 0 of S, but you would find a match starting at index 1 of S.

    Turn in This page, and a printout of your program, along with a screen capture showing it run on the above examples. Remember to make sure you never try to index a character outside of the string! Also submit via the submit script as:
    ~/bin/submit -c=IC210 -p=Hw20 hw20.cpp
    Please fill in: After submitting my hw, I passed ______________ (all/some/none) of the tests.
Additional Examples:
~/$ ./prog
Enter target string: isp
Enter test string  : mississippi
The target isp is not a substring of the test string mississippi!
~/$ ./prog
Enter target string: sip
Enter test string  : mississippi
The target sip is a substring of the test string mississippi!
~/$ ./prog
Enter target string: issi
Enter test string  : mississippi
The target issi is a substring of the test string mississippi!
~/$ ./prog
Enter target string: sss    
Enter test string  : mississippi
The target sss is not a substring of the test string mississippi!