0

If I have a string h ell##!omynam493eisj2ohn ( I've removed all whitespaces, digits, punctuation and etc... using regex) to get: hellomynameisjohn

Now what I want to do is for every n character - so say the minimum length of a word is 4, so for every 4 characters I want to add in a space and increment count. This is what I got up to so far.

    public static int countWords(String original, int minLength){

    original = original.replaceAll("[^A-Za-z]","").replaceAll("[0-9]", "");
    System.out.println(original);

    System.out.println(original.length());

    int count = 0;

    return count;
}

4 characters = 1 word. Therefore, for every 4 characters add in a space and increment the count by 1. I'm not sure on how I can convert this logic into code. I was thinking about using a for-loop but this is how far I got.

    for(int i = 0; i<original.length; i++){

    }

Any suggestions?

px06
  • 2,256
  • 1
  • 27
  • 47

1 Answers1

0

Your method just wants to count how many words are inside with minLength?

 public static int countWords(String original, int minLength){

    original = original.replaceAll("[^A-Za-z]","").replaceAll("[0-9]", "");
    System.out.println(original);

    System.out.println(original.length());

    int count = (original.length()+minLength-1) / minLength;

    return count;
}