Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created September 2, 2015 21:57
Show Gist options
  • Select an option

  • Save jweinst1/2dffb8bde02005d7991c to your computer and use it in GitHub Desktop.

Select an option

Save jweinst1/2dffb8bde02005d7991c to your computer and use it in GitHub Desktop.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment