Question: 3 Words write a program that does the following. It prompts the user to enter a list of words, the last word in the list
3 Words write a program that does the following. It prompts the user to enter a list of words, the last word in the list should be "quit". The program computes the total number of characters in all the words that the user entered (excluding quit"). For example, if the user inputs following sequence of words total number of vords quit then the output should be 18. If the user enters quit then the output should be zero. Before we start writing the program, let us look at the sequence of steps involved to solve this problem. If you were to solve this, how would you proceed? Here is a systamatic way: Initially total number of characters is zero. Look at the first word, if that word equals "quit", then there are no words in the list (since the list end with "quit"), so the output is zero. If it is not equal to "quit", then we count the number of characters in that word, and update total number of characters. Now, look at next word. If it equals "quit", then we are done. If not, count number of characters and add it to total number of characters. Look at next word, check if equals "quit and so on. At any point of time, we are looking at a word-lets call this word currentWord. If currentWord does not equal to "quit", then count number of characters in current Word and update total number of characters. Look at the next word. Now it becomes the currentWord. We repeat this process. We are repeating the following process. Count number of characters in currentWord. Update total number of characters. Look at the next word, it becomes currentWord How long we are repeating? As long as currentWord is not equal to "quit". Thus we arrive at the following pseudo code: 1. Prompt the user to enter a list of words that end with "quit" 2. Set variable totalChar to zero. 3. Read a word. Store it in a variable named currentWord 4 As long as currentWord does not equal "quit Count number of characters in currentVord and store in a variable mum Update totalChar by adding nun Rend next word, and store the value in variable current Vord 5. Output totalChar We can translate Step 4 in to Java code using while loop. This will translate into
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
