subject

The file count. java contains the skeleton of a program to read in a string (a sentence or phrase) and count the number of blank spaces in the string. the program currently has the declarations and initializations and prints the results. all it needs is a loop to go through the string character by character and count (update the countblank variable) the characters that are the blank space. since we know how many characters there are (the length of the string) we use a count controlled loop—for loops are especially well-suited for this.
1. add the for loop to the program. inside the for loop you need to access each individual character—the charat method of the string class lets you do that. the assignment statement
ch = phrase. charat(i);
assigns the variable ch (type char) the character that is in index i of the string phrase. in your for loop you can use an assignment similar to this (replace i with your loop control variable if you use something other than i). note: you could also directly use phrase. charat(i) in your if (without assigning it to a variable).
2. test your program on several phrases to make sure it is correct.
3. now modify the program so that it will count several different characters, not just blank spaces. to keep things relatively simple we'll count the a's, e's, s's, and t's (both upper and lower case) in the string. you need to declare and initialize four additional counting variables (e. g. counta and so on). your current if could be modified to cascade but another solution is to use a switch statement. replace the current if with a switch that accounts for the 9 cases we want to count (upper and lower case a, e, s, t, and blank spaces). the cases will be based on the value of the ch variable. the switch starts as follows—complete it.
switch (ch)
{
case 'a':
case 'a': counta++;
break;
case
}
note that this switch uses the "fall through" feature of switch statements. if ch is an 'a' the first case matches and the switch continues execution until it encounters the break hence the counta variable would be incremented.
4. add statements to print out all of the counts.

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 04:00
Which of the following kinds of programs displays an online advertisement in a banner or pop-up window on webpages, email, or other internet service? e
Answers: 2
question
Computers and Technology, 22.06.2019 23:30
To check spelling errors in a document, the word application uses the to determine appropriate spelling. internet built-in dictionary user-defined words other text in the document
Answers: 1
question
Computers and Technology, 23.06.2019 13:30
What is the primary difference between the header section of a document and the body? a. the body is displayed on the webpage and the header is not. b. the header is displayed on the webpage and the body is not. c. the tag for the body is self-closing, but the tags for the headers must be closed. d. the tag for the header is self closing, but the tag for the body must be closed.
Answers: 3
question
Computers and Technology, 23.06.2019 15:00
In the blank libreoffice writer document, to start the process of entering a date field into a letter, click on the insert menu. edit menu. file menu. fields menu.
Answers: 3
You know the right answer?
The file count. java contains the skeleton of a program to read in a string (a sentence or phrase) a...
Questions
question
Mathematics, 19.03.2020 01:59
question
Spanish, 19.03.2020 02:00
Questions on the website: 13722361