Hi,
Just a small excerise for me.
A program that counts number of words in a string.
The program which i have written now is -
But this counts even extra blank spaces. Like this string "I am new to JAVA",
or " I am new to java" should count only as 5 words, but the result shows more than that.
Any suggestions to change the logic?
Cheers,
Kishore
Just a small excerise for me.
A program that counts number of words in a string.
The program which i have written now is -
Code:
import java.io.*; class countword { private static BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in )); public static void main(String arg[]) throws IOException { System.out.println("Enter a string: "); String input = stdin.readLine(); String[] arr = input.split(" "); int length = arr.length; System.out.println ("Lenght of string: "+input+" is:"+length); } }
or " I am new to java" should count only as 5 words, but the result shows more than that.
Any suggestions to change the logic?
Cheers,
Kishore
Comment