Question: 5.34 Checkpoint B: Traversing a string and analyzing its characters (functions) Given a line of text as input, output the number of Vowels, Consonants, Digits
5.34 Checkpoint B: Traversing a string and analyzing its characters (functions)
Given a line of text as input, output the number of Vowels, Consonants, Digits and Special characters.
Definitions:
- A consonant is any alphabetic character that's not a vowel
- A special character is any character that is neither alphabetic nor a digit.
Ex: If the input is:
Fallis@wesomein$onomaCounty!
the output is:
Vowels: 11 Consonants: 14 Digits: 0 Special characters: 3
Step 1. Getting started with refactoring
Refactoring code means to restructure it to make it easier to read, modify, and potentially extend. At this point, your main tool for refactoring is adjusting the way you break a task into functions.
In this part of the project, you're going to replace a lot of the code in main() with calls to functions that you will define yourself. These instructions will walk you through this first function in a little more detail, and then you will build the others only from specifications.
The first piece of refactoring you'll do is to move the lines of code that print the 4-line report at the end of your program into their own function, which we'll call printReport(). Before you go any further, decide what the function's signature -- that is, the part of its definition that includes the return types and parameter list -- should look like. Write it down or type it somewhere. When you think you have it, highlight the whitened text below to see the text hidden in it. void printReport(int v, int c, int d, int s);.
Your parameter names and their order can differ from mine, but the types should match.
Next, move around code in your program so that this function actually prints the report, and main() just calls it. You should put the function's prototype (that is, the contents of the black box or their equivalent) at the beginning of your program and the actual definition after main(). Here's pseudocode:
// Include necessary libraries // Prototype for writeReport goes here: ___ printReport( /* params */ ); int main() { // most of your code printReport( /* whatever you pass it */); // call the new function return 0; } ___ printReport( /* params */ ) { // Print the counts of vowels, consonants, digits, and special characters // If you do this right, the output shouldn't change at all } Troubleshooting If you have trouble getting above to work, troubleshoot using the following questions:
- Does your function have a prototype that appears above main() in your code?
- Is your function actually defined anywhere? This requires the information in the prototype, followed by curly braces that contain the actual code for the function.
- Is the prototype identical to the first part of the function definition (the part before the curly braces)?
- Does the prototype end with a semicolon?
- Within each of your functions (you have 2 so far), are you being consistent with local variable names? Your vowel counter (for example) does not need to have the same name in main() that it does in printReport, but you should be consistent within each one. When you're refactoring, you will often have to adjust variable names when they move from one function to another.
- Are you making the mistake of declaring local variables that shadow your function parameters? In general, whenever you use a variable, can you point to where it was declared and confirm that it's in the right scope?
- Are you passing arguments to your function in the right order? Remember, it doesn't matter what their names are: the first argument you use will get passed as the first parameter, and so on.
- Are you actually calling your function and, where appropriate, actually using its return value?
Step 2. More refactoring
Here are the other functions you should define and call. For each one, come up with what you think the prototype should look like before highlighting my greyed-out sample prototype.
getUserInput: This function should print the prompt, get the user's input, and return that input. Prototype: string getUserInput();.
When you have defined this function, be sure to call it at the appropriate place in main, and be sure to use the return value (if applicable).
isVowel: This function should return true if a given character is a vowel and false otherwise. It shouldn't assume that the character is uppercase/lowercase. Prototype: bool isVowel(char c);.
In your main() function, you should see a call to each of these functions:
- getUserInput
- isVowel
- printReport
#include
int main() {
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
