-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCountsOfString.java
More file actions
30 lines (27 loc) · 874 Bytes
/
WordCountsOfString.java
File metadata and controls
30 lines (27 loc) · 874 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
/**
* Below program demonstrate to count the no of words in String.
*/
package com.practice.java;
import java.util.HashMap;
import java.util.Scanner;
public class WordCountsOfString {
public static void main(String[] args) {
String str = "";
System.out.println("Enter a String:");
Scanner scan = new Scanner(System.in);
str = scan.nextLine();
String[] split = str.split(" ");
int count = 1;
HashMap<String,Integer> m = new HashMap<String, Integer>();
for(int i=0; i< split.length;i++) {
if(m.containsKey(split[i])) {
count = m.get(split[i]);
m.put(split[i],count+1);
} else {
m.put(split[i],1);
}
}
System.out.println(m);
}
}
// https://www.softwaretestinghelp.com/java-coding-interview-programs/