-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCount.java
More file actions
36 lines (29 loc) · 1008 Bytes
/
WordCount.java
File metadata and controls
36 lines (29 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class WordCount {
public static int wordCount(String s) {
int result = 0;
//if string is just one word
if (s.length() > 0 && s.charAt(0) != ' ') { //first character of string is not a space (is a letter), add to count
result++;
}
//if end of string is space
if (s.length() > 0 && s.charAt(s.length()-1) == ' ') { //last character of string is a space, dont count
result--;
}
//counts number of times a loop of spaces is ended by a character
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') { //when a character at i is a space
while ((i < s.length()) && (s.charAt(i) == ' ')) { //until the character is no longer a space, THEN add to count
i++;
}
result++;
}
}
return result;
}
public static void main(String[] args) {
System.out.print(wordCount("This is a test"));
System.out.print(wordCount("This is a test"));
System.out.println(wordCount(" "));
System.out.println(wordCount("Wahoo42?!?!?!?!?"));
}
}