By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,554 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,554 IT Pros & Developers. It's quick & easy.

count number of words in a string

P: 4
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 -
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. class countword 
  3. {
  4.     private static BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
  5.  
  6.     public static void main(String arg[]) throws IOException  {
  7.     System.out.println("Enter a string: ");
  8.     String input = stdin.readLine();
  9.  
  10.     String[] arr = input.split(" ");
  11.     int length = arr.length;
  12.     System.out.println ("Lenght of string: "+input+" is:"+length);
  13.    }
  14. }
  15.  
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
Jan 12 '07 #1
Share this Question
Share on Google+
12 Replies


10K+
P: 13,264
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 -

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);
}
}


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
From
Expand|Select|Wrap|Line Numbers
  1.  String[] arr = input.split(" ");
then you do
Expand|Select|Wrap|Line Numbers
  1.  int count = 0; 
  2. for(int i = 0; i <arr.length;i++) {
  3. if(arr[i].equals(" ")) {
  4. }
  5. else {
  6.     count++;
  7. }
  8. }
  9.  
and
Expand|Select|Wrap|Line Numbers
  1.  int length = count;
Jan 12 '07 #2

P: 4
hey,
that was a very quick reply!! Really unexpected and great ;-)

it works fine.

Thanks a lot!

Cheers,
Kishore
Jan 12 '07 #3

10K+
P: 13,264
hey,
that was a very quick reply!! Really unexpected and great ;-)

it works fine.

Thanks a lot!

Cheers,
Kishore
Anytime Kishore. Just remember to use code tags next time you post your code either in helping others or in asking more questions
Jan 12 '07 #4

P: 4
Anytime Kishore. Just remember to use code tags next time you post your code either in helping others or in asking more questions
I am a newbie to this forum, but i get what u meant :-)

hey, is there any other method WITHOUT USING SPLIT() for this program?
Jan 12 '07 #5

10K+
P: 13,264
I am a newbie to this forum, but i get what u meant :-)

hey, is there any other method WITHOUT USING SPLIT() for this program?
Combination of split and a regular expression is the best approach. The tokenizer route is becoming archaic
Jan 12 '07 #6

P: 4
Combination of split and a regular expression is the best approach. The tokenizer route is becoming archaic
No clue how to go about it...HELP!!!
Jan 12 '07 #7

10K+
P: 13,264
No clue how to go about it...HELP!!!
You'd need to go through a regular expression tutorial first. One is found here
Jan 12 '07 #8

P: n/a
Looks like the solution is instead to use input.split("\\s+") to get the count instead of input.split(" ") and then just output the length.
Nov 14 '10 #9

P: 3
"one&two#three|four" will return as 4 words.
"I'm here" 3 words and so on.
Just didn't think " " is enough to separate words.

Expand|Select|Wrap|Line Numbers
  1. public static int countWords(String s) {
  2.     int counter = 0;
  3.     boolean word = false;
  4.     int endOfLine = s.length()-1;
  5.  
  6.     for (int i = 0; i < s.length(); i++) {
  7.       //if the char is letter, word = true.
  8.       if (Character.isLetter(s.charAt(i)) == true && i != endOfLine) {
  9.         word = true;
  10.       //if char isnt letter and there have been letters before (word == true), counter goes up.
  11.       } else if (Character.isLetter(s.charAt(i)) == false && word == true) {
  12.         counter++;
  13.         word = false;
  14.       //last word of String, if it doesnt end with nonLetter it wouldnt count without this.
  15.       } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
  16.         counter++;
  17.       }
  18.     }
  19.     return counter;
  20.   }
Nov 16 '10 #10

P: 1
Try splitting with Regular Expressions instead of using " " as delimiter:
Expand|Select|Wrap|Line Numbers
  1. String[] arr = input.split("\\s+");
  2.      int length = arr.length;
That code will split String on any space character (space \n \r or \t) (if there is more characters in a row it will split only once).
Nov 23 '11 #11

P: 1
Could use the following code:

import java.io.*;

public class Countstringwords {

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();
int i,l = 0;
String[] arr = input.split(" ");
int length = arr.length;
for(i=0;i<length;i++){
System.out.println(arr[i]);
if(!"".equals(arr[i])){l++;}

}
System.out.println ("Lenght of string: "+input+" is:"+l);
} }
Jan 10 '12 #12

P: 1
can u explain that.,please.,i need explanation.,tnx
Feb 23 '12 #13

Post your reply

Sign in to post your reply or Sign up for a free account.