Skip to content

Commit df20459

Browse files
committed
Counting the number of words in string
1 parent b0027cd commit df20459

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

countwords.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1+
import java.util.Scanner;
12
class CountTheWords
23
{
3-
public static void main(String[] args)
4+
public static void main(String args[])
45
{
56
System.out.println("Enter the string");
6-
77
Scanner sc = new Scanner(System.in);
8-
98
String s=sc.nextLine();
10-
11-
int count = 1;
12-
13-
for (int i = 0; i < s.length()-1; i++)
9+
int count = 1;
10+
for (int i = 0; i < s.length()-1; i++)
1411
{
1512
if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' '))
1613
{
17-
count++;
14+
count++;
1815
}
1916
}
20-
21-
System.out.println("Number of words in a string = "+count);
17+
System.out.println("Number of words in the string = "+count);
2218
}
2319
}

0 commit comments

Comments
 (0)