1public static void main (String[] args) {
2
3 System.out.println("Simple Java Word Count Program");
4
5 String str1 = "Today is Holdiay Day";
6
7 String[] wordArray = str1.trim().split("\\s+");
8 int wordCount = wordArray.length;
9
10 System.out.println("Word count is = " + wordCount);
11}
12
1 String name = "Carmen is a fantastic play"; //arbitrary sentence
2
3 int numWords = (name.split("\\s+")).length; //split string based on whitespace
4 //split returns array - find legth of array
5
6 System.out.println(numWords);
1 String str = "I am happy and why not
2 and why are you not happy and you should be";
3 String [] arr = str.split(" ");
4 Map<String, Integer> map = new HashMap<>();
5
6 for (int i=0 ; i < arr.length ; i++){
7 if (!map.containsKey(arr[i])){
8 map.put(arr[i],1);
9 } else{
10 map.put(arr[i],map.get(arr[i])+1);
11 }
12 }
13 for(Map.Entry<String, Integer> each : map.entrySet()){
14
15 System.out.println(each.getKey()+" occures " + each.getValue() + " times");
16 }
1public static void main(String[] args)
2 {
3 //Scanner object instantiation
4 Scanner dude = new Scanner(System.in);
5
6 //variable declaration
7 String string1 = "";
8 int count = 0;
9 boolean isWord = false;
10
11
12 //user prompt and input
13 System.out.println("Enter in your string");
14 string1 = dude.nextLine();
15
16 int endOfLine = string1.length()-1;
17 char ch [] = string1.toCharArray();
18
19 for (int i = 0; i < string1.length(); i++)
20 {
21 if(Character.isLetter(ch[i]) && i != endOfLine)
22 {//if character is letter and not end of line
23 isWord = true; //it is part of a word
24 }
25 if (!Character.isLetter(ch[i]) && isWord)
26 { //if character is not a letter, and previous
27 //character is a letter i.e. non-letter is
28 //preceded by character
29 count++; //add to word count
30 isWord = false; //get ready to detect new word
31 }
32 if (Character.isLetter(ch[i]) && i == endOfLine)
33 { //if character is letter
34 //and at end of line
35 count++; //add to word count
36 isWord = false;
37 }
38
39 }
40 System.out.println("There are " +count+ " words");
41 }
1// Count words (text separated by whitespace) in a piece of text
2use std::collections::HashMap;
3
4fn word_count(text: &str) -> HashMap<&str, i32> {
5 let mut map = HashMap::new();
6 for word in text.split_whitespace() {
7 *map.entry(word).or_insert(0) += 1;
8 }
9 map
10}
11
12fn main() {
13 println!("Count of words = {:?} ",word_count("the quick brown fox jumped over the lazy dog"));
14}
1 public static int count(String word) {
2 if (word == null || word.isEmpty()) {
3 return 0;
4 }
5
6 int wordCount = 0;
7
8 boolean isWord = false;
9 int endOfLine = word.length() - 1;
10 char[] characters = word.toCharArray();
11
12 for (int i = 0; i < characters.length; i++) {
13
14 // if the char is a letter, word = true.
15 if (Character.isLetter(characters[i]) && i != endOfLine) {
16 isWord = true;
17
18 // if char isn't a letter and there have been letters before,
19 // counter goes up.
20 } else if (!Character.isLetter(characters[i]) && isWord) {
21 wordCount++;
22 isWord = false;
23
24 // last word of String; if it doesn't end with a non letter, it
25 // wouldn't count without this.
26 } else if (Character.isLetter(characters[i]) && i == endOfLine) {
27 wordCount++;
28 }
29 }
30
31 return wordCount;
32 }
33