Counting words of a string

This commit is contained in:
Ashish Agarwal
2017-04-08 18:26:06 +05:30
parent 8c38c60b93
commit b0027cd585
2 changed files with 23 additions and 0 deletions

BIN
.DS_Store vendored

Binary file not shown.

23
countwords.java Normal file
View File

@ -0,0 +1,23 @@
class CountTheWords
{
public static void main(String[] args)
{
System.out.println("Enter the string");
Scanner sc = new Scanner(System.in);
String s=sc.nextLine();
int count = 1;
for (int i = 0; i < s.length()-1; i++)
{
if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' '))
{
count++;
}
}
System.out.println("Number of words in a string = "+count);
}
}