Question
Write a program that asks the user to enter a sentence ended by a period, a question mark, or an exclamation mark and prints out
Write a program that asks the user to enter a sentence ended by a period, a question mark, or an exclamation mark and prints out the number of space characters (spaces, new lines, tabs) in that sentence.
The program should function as follows (the items bolded are to be entered by the user):
Enter a sentence (end by . or ? or !):
This is a sample sentence.
Number of space characters: 2
Number of new line characters: 2
Number of tabs: 1
Note: Above there is a new line before the first word of the sentence and after the word sample, there are two spaces (after is and after a) and a tab (after the word This).
Algorithm design process
What variables are needed and what type?
Need variables to store the count of characters, new_lines and tabs.
Any other variables that you need? A variable of type char to read in the input from the user.
Get input from user
Print out the text to prompt the user
Set up a loop (while, do-while) to read in character by character until a . or ? or ! are found. What should be the logical condition that will enable the loop to run until . or ? or ! are encountered?
Inside the loop
For every character read, see if it is a new line, space or tab, in order to increment the appropriate counter. Ignore any other characters. What is the best way to structure this? A cascaded if-else statement would be too long, so the best option would be a switch statement. Think about structuring the switch statement in the most compact way possible (group cases that go together and think of what should go in the default case).
At the end of the loop print out the number of spaces, new lines and tabs.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started