public int countCode(String str) {
int code = 0;
for(int i=0; i<str.length()-3; i++){
if(str.substring(i, i+2).equals("co") && str.charAt(i+3)=='e'){
code++;
}
}
return code;
}
Hi guys, I've solved this problem by some help among the internet. But the actual problem that I'm facing is this, (str.length()-3) in the for loop. I don't understand why the str.length()-3 having this -3 in it. please explain it...
Inside the for loop, for any index (
i), it checks that the chars atiandi+2andi+3match your requirements. If youribecomes length of your string (or last character), then the code will throw exception since it will try to findcharat position which is not really there.