I have the following assignment: Count how many "runs" of the given character appear in the given string. A "run" is a consecutive block of one or more occurrences of the same character. For example, if the string is "AATGGGGCCGGTTGGGGGGGGGAAGC" and the character is "G", returns 4. No import, '?' is allowed My attempt:
public static int charRunCount(String str, char c){
int counter = 0;
for (int i = 0; i < str.length()-1; i++) {
if ( (str.charAt (i) == str.charAt (i+1)) && str.charAt (i)==c )
counter+=1;
}
return counter;
}
output =12, please help fix or correct.
ifblock:System.out.printf("%d, %d, %c", i, counter, str.charAt(i));You'll then see the counter and character printed for each step of the loop. You'll quickly see what the problem is.