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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 23:00, sarahnd6907
Explain briefly why you cannot expect to find a previous version of every file with which you work.
Answers: 1
image
Computers and Technology, 22.06.2019 12:40, Courtneymorris19
The most complicated four letter word
Answers: 1
image
Computers and Technology, 22.06.2019 13:30, ashleypere99
Jane’s team is using the v-shaped model for their project. during the high-level design phase of the project, testers perform integration testing. what is the purpose of an integration test plan in the v-model of development? a. checks if the team has gathered all the requirements b. checks how the product interacts with external systems c. checks the flow of data in internal modules d. checks how the product works from the client side
Answers: 1
image
Computers and Technology, 23.06.2019 02:30, noah2o2o
These factors limit the ability to attach files to e-mail messages. location of sender recipient's ability to open file size of file type of operating system used
Answers: 2
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 in other subjects:

Konu
Biology, 03.08.2019 10:20
Konu
Geography, 03.08.2019 10:20
Konu
Physics, 03.08.2019 10:20