I tested on my computer with my own tests like:
Case 1:
world?me,lol,world => return 2
Case 2:
world.ok.world.wor32.wor0x:world => return 3
It says "Determine why 'world' appears more than it exists"
I don't want to use the solution that CodeGym provides. Too complex.
package com.codegym.task.task19.task1907;
/*
Counting words
*/
import java.io.*;
public class Solution {
private static char[] world = new char[]{'w','o','r','l','d'};
// private static char[] chars = new char[]{' ', ' ',' ',' ',' '};
private static int result = 0;
private static int counter = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String file = br.readLine();
FileReader fr = new FileReader(file);
while(fr.ready()){
char theChar = (char) fr.read();
// if world[counter] == theChar
// then counter++; if counter == 5 , result++ counter = 0
// else counter = 0;
if(world[counter] == theChar){
counter++;
if(counter == 5){
result++;
counter = 0;
}
} else {
counter = 0;
}
}
br.close();
fr.close();
System.out.println(result);
}
}