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...

An administrator is attempting to troubleshoot a program that is unable to resolve IP addresses from FQDNs. Which of the following commands is used to check reverse DNS settings?

A. dig B. ifconfig C. nbtstat D. route

Computer Science & Information Technology

The function of the ________ layer is to control access to the transmission medium and to provide an orderly and efficient use of that capacity.

A. CRC B. MPDU C. MAC D. MSDU

Computer Science & Information Technology