Merge pull request #21 from ashish4321/master

Added program for counting words in a string.
This commit is contained in:
Chetan Kaushik 2017-04-09 14:58:53 +05:30 committed by GitHub
commit 7b2224a012
2 changed files with 19 additions and 0 deletions

BIN
.DS_Store vendored

Binary file not shown.

19
countwords.java Normal file
View File

@ -0,0 +1,19 @@
import java.util.Scanner;
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 the string = "+count);
}
}