Created
September 2, 2015 21:57
-
-
Save jweinst1/2dffb8bde02005d7991c to your computer and use it in GitHub Desktop.
word count for a string based on number of spaces in java.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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