• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Luciano Mantuaneli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't see non-breaking space in the list.

-- Kaydell
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple way to count tokens of strings:



[ December 09, 2006: Message edited by: Jesse Crockett ]
[ December 09, 2006: Message edited by: Jesse Crockett ]
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hang a left on main. Then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic