Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [20pts] Assume the declarations and function definitions below. 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 testcap(char c) {
      return ('A' <= c && c <= 'Z');
    }
    


    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])
    testcap(s)
    testcap(s[0])
    testcap(s[0] + t[0])
  2. [80Pts] We will write a program that performs Ceaser shift cipher. You can revisit the SY110 notes on shift cipher, if you want. Here are sample runs:
    ~/$ ./shift
    Enter a shift value: 3
    Enter a plaintext message (lower-case letters only): abc
    def
    
    ~/$ ./shift
    Enter a shift value: 3
    Enter a plaintext message (lower-case letters only): capitalize
    fdslwdolch
    
    ~/$ ./shift
    Enter a shift value: 20
    Enter a plaintext message (lower-case letters only): capitalize
    wujcnufcty
    
    Assume that the plaintext always contain only the lower-case letters. I already wrote some code for you. Complete the function definitions that so the result code may work as above.
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    //============================================================
    // prototypes
    // c: input character,  n: shift amount
    char shift(char c, int n);
    
    // w: input string,  n: shift amount
    string shift(string w, int n);
    
    //============================================================
    // main function: I already wrote the easy part for you. 
    int main() {
      // get a shift value from the user
      int n;
      cout << "Enter a shift value: ";
      cin >> n;
    
      // get a plaintext message from the user
      string s;
      cout << "Enter a plaintext message (lower-case letters only): ";
      cin >> s;
    
      // shift cipher!!
      cout << shift(s,n) << endl;
    }
    
    //============================================================
    // function definitions
    char shift(char c, int n) {
      // to do: fill in the code!!
    }
    
    string shift(string w, int n) {
      // to do: fill in the code!!
    }
    
    Turn In: A screen capture of a terminal window showing the program running on the sample inputs from above, and a codeprint printout of the source code for the three programs.