Skip to content

Commit b0a3c0f

Browse files
committed
Word Count
1 parent 058f92b commit b0a3c0f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/easy/WordCount.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package easy;
2+
3+
/**
4+
* Have the function WordCount(str) take the str string
5+
* parameter being passed and return the number of words the string
6+
* contains (e.g. "Never eat shredded wheat or cake" would return 6).
7+
* Words will be separated by single spaces.
8+
*/
9+
public class WordCount {
10+
11+
/**
12+
* Word Count function.
13+
*
14+
* @param str input string
15+
* @return the number of words the string
16+
*/
17+
private static int wordCount(String str) {
18+
String[] words = str.split(" ");
19+
return str.length() > 0 ? words.length : 0;
20+
}
21+
22+
/**
23+
* Entry point.
24+
*
25+
* @param args command line arguments
26+
*/
27+
public static void main(String[] args) {
28+
var result1 = wordCount("The mind was dreaming. The world was its dream.");
29+
System.out.println(result1);
30+
var result2 = wordCount("I have always imagined that Paradise will be a kind of library.");
31+
System.out.println(result2);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)