Assignment 15
Due Thursday, May 31 at 9:20am on Canvas


Assignment 15.1: Lower and Upper Case (10 pts)
  • Let's write a program to do the following:
    • convert a string to all lower-case letters
    • convert a string to all upper-case letters
  • To approach this problem, we need to remember the ASCII table and how it works.
  • Recall that upper case letters have ASCII values between 65 and 90.
  • Lower case letters have ASCII values between 97 and 122.
  • Verify that these facts are correct by looking at the ASCII Table.
  • Finally, note that lower case 'a' is the integer number 97 and upper case 'A' is the integer number 65.
  • Therefore, there is a difference of 32 between the ASCII value of any lower case character and its upper case version.
  • We will use these facts to our advantage in this program.
  • Create a new file called upperLower.cpp.
  • Copy and paste the starter code below into your file:

/*
*name
*section
*/

#include <iostream>
using namespace std;


int main () { 

    string sentence;   

    return 0;

}


  • Add some code to welcome the user, prompt the user to enter a sentence and then store the sentence as a string variable.
  • Verify that your program compiles and runs.
  • Now, let's add some statements.
  • We will need to use a for loop, string indexing and our knowledge of ASCII to complete this task.
  • Write a for loop to cycle through our string variable that was passed in as a parameter to our function.
for (int i = 0; i < sentence.length(); i++) {
    //statements for string processing go in here
}
  • Inside the loop, we need to find out if this is a capital letter or not. We will use an if statement to test whether the ASCII value lies between 65 and 90.
if (sentence[i] >= 65 && sentence[i] <= 90) {//if this character is between 65 and 90, it is a capital

}

  • What should go inside the if statement?
  • Consider that each upper case character has an ASCII value that is 32 less than each lower case character.
  • So, if it is an uppercase letter, we will need to add 32 to its ASCII value and then convert it back to a char.
  • Add the below statements inside the curly braces of the if.
sentence[i] += 32;
//adding 32 to sentence[i]'s ASCII value
  • Now, run your program and verify that it works correctly by looking at the output of the test in main.
  • For the rest of the program, your job is to write the remaining for loop to convert the sentence to all upper case letters.
  • When you are finished, submit your work to Canvas.

Your output should look identical to the output below:


Please enter a sentence: I love cookies!
Your sentence in all upper case: I LOVE COOKIES!
Your sentence in all lower case: i love cookies!


Assignment 15.2: Loopy Characters (10 pts)

  • Counting loops are very commonly used in programming to ... count things. Character data types are used to store and display individual letters and other symbols. In this project we explore both counting loops and characters in C++.
  • Copy and paste the starter code into a file called loopChars.cpp:

#include <iostream>
using namespace std;

int main() {
    int n; // the integer number
    char ch; // the single character
    cout << "** Loopy Characters!**\n\n";
    cout << "Enter an integer between 1 and 20: ";
    cin >> n;

    cout << "Enter a single character: ";
    cin>> ch;

    //Repeating the char n times with a for-loop.
    cout << "#1. Printing " << ch << " " << n << " times:\n";
    // Put your code here
    cout << endl << endl;

    // Printing starting with char and the following n ASCII chars.
    cout << "#2. Printing starting with " << ch << " and the following " << n - 1
         << " ASCII characters:\n";
    // Put your code here
    cout << endl << endl;

    // Repeating the char n times with stars on odd indexes.
    cout << "#3. Printing " << ch << " character " << n
         << " times substituting '*' on odd indexes:\n";
    // Put your code here
    cout << endl << endl;

    // Repeating the character n times with tick marks (+) every 5 chars
    cout << "#4. Printing " << ch << " character " << n
         << " times substituting (+) every fifth character:\n";
    // Put your code here
    cout << endl << endl;

    cout << "#5. Printing " << n << " lines of the previous loop:\n";
    // Hint: put your for-loop from the previous challenge inside another
    // for-loop that has a different counting variable.
    // Put your code here

    return 0;
}


  • User input is already coded into the worksheet, consisting of an integer number, n, and a single character.
  • Do not add any other input commands or change the input order.
  • (#1) Use a for-loop to print the character entered by the user the number of times specified by the integer number typed in. See the Example Run for an example.
  • (#2) Use a second for-loop to print the character entered by the user followed by the subsequent n - 1 characters in the ASCII table. See the Example Run for an example.
    • Hint: In this loop, we are adding one to the ASCII value of a character each time the for loop executes (add one more to the char's value each time the loop executes)
  • (#3) Use a third for-loop to print the character n / 2 times with a '*' instead of the character on even counts of the loop as shown in the Example Run.
    • Hint: Use and if-statement to test for even or odd counts of the loop.
  • (#4) Use a fourth for-loop to print the character n times with a tick mark '+' substituted every fifth character as shown in the Example Run.
    • Hint: Use and if-statement to test for the every fifth count of the loop. Start the for-loop count from 1, but check the end condition! Only print the number if nothing else is printed.
  • (#5) Use yet another loop to print the previous loop n times. Thus, this problem should use two loops.
  • Hint: put your for-loop from the previous challenge inside another for-loop that has a different counting variable. Print a newline character inside the outer loop after the inner loop completes.
  • Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.


**Loopy Characters!**

Enter an integer between 1 and 20: 10

Enter a single character: A

#1. Printing A 10 times:
AAAAAAAAAA

#2. Printing starting with A and the following 9 ASCII characters:
ABCDEFGHIJ

#3. Printing A character 10 times substituting '*' on even indexes:
A*A*A*A*A*

#4. Printing A character 10 times substituting (+) every fifth character:
AAAA+AAAA+

#5. Printing 10 lines of the previous loop:
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+

    

  • In the above example run, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

  • After displaying the output, exit the program. Submit your file to Canvas when you are finished.