Free Version
Difficult

Debugging Code: Replace Z With S in String

APCSA-P4PGYG

Consider the following method.

// Return the string replacing every z with an s
public static String replaceZwithS(String phrase)
{
String out="";
for (int i=0; i<phrase.length(); i++)
{
String c = phrase.substring(i,i+1);
if (phrase.substring(i, i+1).equals("z"));
c = "s";
out += c;
}
return out;
}


The following code from the main method should produce the output "busser", but instead produces "ssssss".

System.out.println(replaceZwithS("buzzer"));


Which of the following BEST describes why this error is occurring?

A

The if statement is always true, so every character is replaced with an "s".

B

The for statement itself is incorrect.

C

The semi-colon at the end of the if statement means that "s" will always be assigned to c.

D

c should only be concatenated to out when the character is not a "z".

E

The following statement is incorrect: String c = phrase.substring(i,i+1);.