jweinst1
9/2/2015 - 9:57 PM

word count for a string based on number of spaces in java.

word count for a string based on number of spaces in java.

import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    System.out.println(wordcount(statement));
  }
     static String statement = "The apples are red.";
    static int wordcount(String x) {
        char[] charlist = x.toCharArray();
        int count = 0;
        for(char space: charlist) {
            if (space == ' ') {
                count++;
            }
        }
        return count + 1;
    }
}