This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How we can Count  the no.of words in a given String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How we can Count  the no.of words in a given String" Watch "How we can Count  the no.of words in a given String" New topic
Author

How we can Count the no.of words in a given String

Jalli Venkat
Greenhorn

Joined: Aug 10, 2006
Posts: 27
Hi Friends,
I want pseudo code and logic for counting the no. of words in a given String.
Eg: Sting str="welcome to the javaranch big mouse saloon";

This is my string.i want 2 count the no. of words in that String.


venkat
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

You can use class java.util.StringTokenizer to break a sentence into separate words, and a loop to count the number of words.

Lookup StringTokenizer in the Java API documentation and try using it in your program.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Karan Rajan
Greenhorn

Joined: May 19, 2005
Posts: 12
A quick and easy way is to use StringTokenizer with one whitespace " " as the delimiter. Calling countTokens() will give you the number of words.

e.g.

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
Starting with JDK 1.4, the String class has a method called split() that will split the String into an array of tokens using a regex as delimiter.
Luciano Mantuaneli
Greenhorn

Joined: Dec 04, 2006
Posts: 13
It has to be pseudo code? Because I know a solution using features too specific of Java API. Can't see how to express it in pseudo code... Let me try:

If you want to see the code, let us know!


[]'s<br />Mantu<br /> <br />Sorry for my poor english...<br /> <br /><i>Time after time we lose sight of the way. Our causes cant see their effects</i> - Neil Peart
Luciano Mantuaneli
Greenhorn

Joined: Dec 04, 2006
Posts: 13
Originally posted by Keith Lynn:
Starting with JDK 1.4, the String class has a method called split() that will split the String into an array of tokens using a regex as delimiter.


This approach can be very tricky: Depending on your regex, you may have distorted values
[ December 04, 2006: Message edited by: Luciano Mantuaneli ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

If you're just talking about simple English strings like the example (and this is a beginner's homework assignment) then the StringTokenizer and String.split() methods will work just fine. But in real life, especially in languages like Japanese and Thai, extracting words from strings is a rather difficult task.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 679

What if the String has other white-space in it, such as a non-breaking space, a tab, a carriage-return, a linefeed, or a formfeed?

Are there other kinds of whitespace?

-- Kaydell
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

Originally posted by Luciano Mantuaneli:
If you want to see the code, let us know!

Luciano, please don't post the complete solution - that way Jalli will not learn anything. Have you read the description of the beginner forum? It says:

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Luciano Mantuaneli
Greenhorn

Joined: Dec 04, 2006
Posts: 13
Originally posted by Jesper Young:

Luciano, please don't post the complete solution - that way Jalli will not learn anything. Have you read the description of the beginner forum? It says:


Got it!

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
Originally posted by Kaydell Leavitt:
What if the String has other white-space in it, such as a non-breaking space, a tab, a carriage-return, a linefeed, or a formfeed?

Are there other kinds of whitespace?

-- Kaydell


\s mathches A whitespace character: [ \t\n\x0B\f\r].

You can find a complete list in the API docs for java.util.regex.Pattern
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 679

I didn't see non-breaking space in the list.

-- Kaydell
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
word counting can be done in easy way by using JAVA API or you can use your own algorithm like counting the character and if next character is space then increment the counter. you can check for other cases like if there is more than single space or string starts with a space or ends with more than one space.
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
A simple way to count tokens of strings:



[ December 09, 2006: Message edited by: Jesse Crockett ]
[ December 09, 2006: Message edited by: Jesse Crockett ]
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
From the Javadoc of StringTokenizer:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.


It is effectively deprecated (though not officially marked as such) and should really not be used in new code.


42
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How we can Count the no.of words in a given String
 
Similar Threads
variable problem
searching
read words from file into array
Best practice to count a word inside a String
Counting exact matches of substring.