Assignment 16
Due Tuesday, June 5 at 9:20am on Canvas
Assignment 16.1: 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 string.length(), string indexing using the square
bracket syntax, and a for loop to correctly solve this problem.
- Open up a new C++ program named 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 Canvas.
The output of your program should look identical to the sample output below.
It should also count capital letter vowels like this:
Assignment 16.2: Vowels Again- Write a function with the following signature:
int countVowels(string str) The function returns a count of the letters in the string parameter that are vowels (A, E, I, O, U, a, e, i, o, u)
- Hint: You've done this same thing on your previous assignment, just not with a function.
- For this assignment we are going to "outsource" the work of counting vowels to a function named countVowels.
Part 1:
- Copy and paste the starter code into a file named vowels.cpp:
/** * Name * Section */
int countVowels(string s) { //add the code for your function here
}
int main() { string sentence; int numVowels; //Prompt user to enter a sentence //call the countVowels function, passing it the sentence as a parameter //& storing result as numVowels //print out the number of vowels in the sentence cout << "There are " << numVowels << " in your sentence." << endl;
}
- Your program should first prompt the user to enter a sentence.
- Hint: Use getline().
- Then, you should pass the sentence variable into your function and store the return value in your numVowels variable.
- Test out your code with various sentences.
- The program should now work like this:
Enter a sentence: I love cookies a lot! There are 9 vowels in your sentence.
Part 2: - Add a while loop to your code to repeatedly prompt the user to enter a sentence or q to quit.
- The program should accept q, Q, quit, or Quit as signals to exit the loop.
- When you are finished, your program should work like this:
Enter a sentence or q to quit: I love cookies a lot! There are 9 vowels in your sentence. Enter a sentence or q to quit: I wouldn't mind a piece of cake, either. There are 14 vowels in your sentence. Enter a sentence or q to quit: Q Goodbye! Part 3: - Next, add another function to your program to count letters in the sentence.
- Your function signature should look like this:
int countLetters(string s) - Thus, add a new variable to the top of main that will store the result of calling the above function.
int numLetters;
- To write the above function, you may need to consult the ASCII table. Hint: what is the range of ASCII values in which you would find letters?
- After
you write the above function, call it inside of main and then add to
your print statement to display the result for the number of letters in
the sentence.
- Your program should now work like this:
Enter a sentence or q to quit: I love cookies a lot! There are 9 vowels in your sentence, and 16 letters.
Enter a sentence or q to quit: I wouldn't mind a piece of cake, either. There are 14 vowels in your sentence, and 30 letters.
Enter a sentence or q to quit: Q Goodbye! - When you are certain it is working properly submit to Canvas.
|
|