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, 23.06.2019 00:10
Write a function so that the main0 code below can be replaced by the simpler code that calls function mphandminutes tomiles0. original main0 int main) l double milesperhour-70.0; double minutestraveled = 100.0; double hourstraveled; double milestraveled; hourstraveled = minutestraveled / 60.0; milestraveled = hourstraveled * milesperhour; cout < "miles" 2 using namespace std; 4 /* your solution goes here/ 6 int maino 1 test passed 7 double milesperhour 70.0 all tests passed 8 double minutestraveled 100.0; 10 cout < < "miles: " < < mphandminutestomiles(milesper-hour, minutestraveled) < < endl; 12 return 0; 13
Answers: 1
question
Computers and Technology, 23.06.2019 00:30
Which one of the following is considered a peripheral? a software b mouse c usb connector d motherboard
Answers: 2
question
Computers and Technology, 23.06.2019 19:30
You can apply several different worksheet themes from which tab?
Answers: 1
question
Computers and Technology, 24.06.2019 03:30
Explain the importance of html in web page designing in 20 sentences..
Answers: 1
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, 12.01.2021 15:30
question
Mathematics, 12.01.2021 15:30
question
Mathematics, 12.01.2021 15:30
question
Mathematics, 12.01.2021 15:30
Questions on the website: 13722367