Hello, I am still new to the Java programming language and am having an issue with a code I have written. If possible, I would like some help with identifying why my code is not working for all instances.
The problem is the following:
Count the number of "xx" in the given string. We'll say that overlapping is allowed, so "xxx" contains 2 "xx".
countXX("abcxx") ¿ 1
countXX("xxx") ¿ 2
countXX("xxxx") ¿ 3
This is my code that I have written:
int countXX(String str) {
String check = "xx";
int count = 0;
for (int i = 0; i < str.length(); i++){
if (str.substring(i, i+1).equals(check)){
count++;
}
else count = count;
}
return count;
}
My code so far only returns 0 as the answer. Thank you for any help you can provide.