- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCount.java
More file actions
Latest commit
20 lines (17 loc) · 518 Bytes
/
Copy pathWordCount.java
File metadata and controls
20 lines (17 loc) · 518 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
packagecom.sarvesh.javabasics;
importjava.util.Scanner;
publicclassWordCount {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.print("Enter a sentence: ");
Stringstr = sc.nextLine();
intcount = 0;
for(inti=0;i<str.length();i++) {
if(str.charAt(i) != ' '&& (i==0||str.charAt(i-1)==' ')) {
count++;
}
}
System.out.println("Words = " + count);
sc.close();
}
}