Wednesday, April 2, 2014

Java program to count number of words

import java.lang.*;
import java.io.*;
import java.util.*;
class WordCount
{
private static String line="";
private static BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
private static StringTokenizer toks=new StringTokenizer(line);
public static void main(String[] args) throws Exception
{
int cnt=0;
line=br.readLine();
toks=new StringTokenizer(line);
while(toks.hasMoreTokens())
{
toks.nextToken();
cnt++;

}
System.out.println("\n\nThe Number of Words in the line was = "+cnt+"\n\n");
}
}

No comments:

Post a Comment