The following code containing a loop attempts to find how many times the letter ‘r’ appears in a string. But something is wrong with the loop. How would you fix it?


String s = “railroad”;
int count = 0;
char letter = s.charAt(index);
for (int index = 0; index < s.length(); ++index)
if (letter == ‘r’)
++count;


The assignment to letter needs to be moved down into the loop right before we check to see if the letter
is an r.

Computer Science & Information Technology

You might also like to view...

Most database tables have a(n) ____________, which is a column that can be used to identify a specific row.

a. identifier b. primary key c. key word d. column name

Computer Science & Information Technology

What is wrong with the following function template (Other than the fact that it really does almost nothing)?

``` template int f( int & x ) { return x; } int main() { int y = 3, z; z = f(y); } ```

Computer Science & Information Technology

When you click a text box border, _____.?

A. ?the sizing handles disappear B. ?the border disappears C. ?it changes to a dotted line D. ?it changes to a solid line

Computer Science & Information Technology

The Decrease Indent and Increase Indent buttons change the indents by ________ tab intervals

A) 1/4 inch B) 1/2 inch C) 1 inch D) 2 inch

Computer Science & Information Technology