forked from sathishmepco/Java-Interview-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountWords2.java
47 lines (39 loc) · 1 KB
/
CountWords2.java
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
37
38
39
40
41
42
43
44
45
46
47
package com.java.strings;
import java.util.Scanner;
/*
* Count the number of Words using split method
* ---------------------------------------------
* Write the java program to count the number of
* words in the given string using split method
*
* string.split("\\s");
* The above method returns the no of words
*
* say Given String : java programming
* no of words : 2
* say Given String : code
* no of words : 1
*
*/
public class CountWords2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter any String : ");
String str = scanner.nextLine().trim();
String words[] = str.split("\\s");
int wordCount = words.length;
System.out.println("Number of words : "+wordCount);
scanner.close();
}
}
/*
OUTPUT
Enter any String : java programming
Number of words : 2
Enter any String : 123 123
Number of words : 2
Enter any String : code
Number of words : 1
Enter any String : I am a good coder
Number of words : 5
*/