-
Notifications
You must be signed in to change notification settings - Fork 110
/
Count_Word.java
47 lines (41 loc) · 1.05 KB
/
Count_Word.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
/* Count number of word in a string
*Note : Always ignore the spaces if more than 1 space given between two word
Sample Input : 1
------------------------
Kalyani Goverment Engineering College
Process
---------------------------
[Kalyani, Goverment, Engineering, College]
Output
---------------------------
Number of word : 4
*************************************
Sample Input : 2
------------------------
Kalyani Goverment Engineering College
Process
---------------------------
[Kalyani, Goverment, , Engineering, College]
Output
---------------------------
Number of word : 4
*/
import java.util.*;
public class Count_Word
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] arr = s.split(" ");
int count=0;
//System.out.println(Arrays.toString(arr));
for (int i = 0; i < arr.length; i++) {
if(arr[i]!="")
{
count++;
}
}
System.out.println("Number of word : "+count);
}
}