Skip to content

Commit b0027cd

Browse files
author
Ashish Agarwal
committed
Counting words of a string
1 parent 8c38c60 commit b0027cd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

.DS_Store

2 KB
Binary file not shown.

countwords.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class CountTheWords
2+
{
3+
public static void main(String[] args)
4+
{
5+
System.out.println("Enter the string");
6+
7+
Scanner sc = new Scanner(System.in);
8+
9+
String s=sc.nextLine();
10+
11+
int count = 1;
12+
13+
for (int i = 0; i < s.length()-1; i++)
14+
{
15+
if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' '))
16+
{
17+
count++;
18+
}
19+
}
20+
21+
System.out.println("Number of words in a string = "+count);
22+
}
23+
}

0 commit comments

Comments
 (0)