Assignment 14
Due Wednesday, November 11 at 9:00am on Catalyst

Assignment 14.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.
  • Open up CodeBlocks and 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.
  • At the top of main, below the declaration of the sentence variable, declare a local integer variable called currentChar
int currentChar;
  • This variable will used to store our conversion of each character in the string to an integer value.
  • Next, below currentChar, 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 wish to convert each character to an integer so we can see if it falls in the range for an upper case character (upper case characters have ASCII values between 65 and 90).
  • Add a statement like the one below:
currentChar = (int) sentence[i];
  • Next, we need to find out if this is a capital letter or not. We will use an if statement to test whether this integer value lies between 65 and 90.
if (currentChar >= 65 && currentChar <= 90) {

}
  • 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.
currentChar += 32;
sentence[i] = (char) currentChar
  • Now, run your program and verify that it works correctly by looking at the output of the test in main.
  • 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 Catalyst

Your output should look identical to the output below:





Assignment 14.2: Vowels (10 pts)
  • Write a program that takes in a sentence from the user and then counts the number of vowels in the sentence.
  • You will need to use getline(), the string.length() function, and a for loop to correctly solve this problem in C++.
  • Open up a new C++ file called vowels.cpp.
  • First, welcome the user to the program and explain its purpose:
Welcome! Give me a sentence and I will count its vowels.
  • Then prompt the user to enter a sentence.
Please enter a sentence: _
  • Store the user input as a string variable.
  • Now, using a for loop and the string.length() function, count how many vowels (a, e, i, o, u) there are in the sentence.
  • Note that the program should also count capital vowels (A, E, I, O, U).
  • Finally, output the number of vowels like so:
There are 5 vowels in your sentence.
  • Make sure that your sample output matches mine.
  • Then, submit your program to Catalyst.

The output of your program should look identical to the sample output below.


It should also count capital letter vowels like this: