# Find Number of Words in a String

• (3 Pages)
• 1
• 2
• 3

## 30 Replies - 31102 Views - Last Post: 24 July 2010 - 09:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=182822&amp;s=f4e540b546e060f8a5b8cb30eecbe757&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rakesh0

Reputation: -3
• Posts: 11
• Joined: 23-July 10

# Find Number of Words in a String

Posted 23 July 2010 - 04:56 AM

m new to programming i want to find no. of words in any string so can anybody have any logic if yas then tell me,,,,
Is This A Good Question/Topic? 0

## Replies To: Find Number of Words in a String

### #2 rakesh0

Reputation: -3
• Posts: 11
• Joined: 23-July 10

## Re: Find Number of Words in a String

Posted 23 July 2010 - 05:14 AM

please give me some code for that purpose,,,,,,

### #3 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

## Re: Find Number of Words in a String

Posted 23 July 2010 - 05:27 AM

Number of words...maybe try using the split() method and split according to whitespaces?

And count the length of the array returned by that method:

```String[] words = sentence.split(" ");
//words.length is your # of words

```

### #4 rakesh0

Reputation: -3
• Posts: 11
• Joined: 23-July 10

## Re: Find Number of Words in a String

Posted 23 July 2010 - 05:35 AM

i still not getting the output can you send me the code,,,,??? piz m new to programming

### #5 m-e-g-a-z

• Winning

Reputation: 497
• Posts: 1,457
• Joined: 19-October 09

## Re: Find Number of Words in a String

Posted 23 July 2010 - 05:49 AM

We will not write code for you, feel free to provide some code you have done to show us that you have put effort in.

[rules][/rules]

### #6 eZACKe

• Garbage Collector

Reputation: 120
• Posts: 1,278
• Joined: 01-June 09

## Re: Find Number of Words in a String

Posted 23 July 2010 - 05:52 AM

rakesh0, on 23 July 2010 - 08:35 AM, said:

i still not getting the output can you send me the code,,,,??? piz m new to programming

Which is exactly why we shouldn't send you the code. If you just always have code handed to you then you will not learn. Post what you've done. You said you're not getting the desired output so you must have done something.

Let's see it!

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:02 AM

Title renamed to be more descriptive. Please respect rule #4 of this forum. "Java" is not a helpful title in a forum dedicated to Java Programming.

Also, I want to reiterate rule #1. We will be happy to help you debug code you wrote, but we will not give you the code.

### #8 Cuzzie

• D.I.C Regular

Reputation: 72
• Posts: 342
• Joined: 16-July 10

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:04 AM

Here's a hint. Do what bcranger said, and use a for loop to iterate through the "word" array, use equals() method to find which word is equal to the word you want to find, and increase a "count" variable when the word is equal. I hope this is clear enough to give you a headstart. Well, actually it's clear enough to let you write the whole app. XD

### #9 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:13 AM

I think he just wants the number of words. If you provide a string to that snippet bcranger gave you, it works out the number of words for you. All you need then is a System.out.println(???????) to display them.

### #10 m-e-g-a-z

• Winning

Reputation: 497
• Posts: 1,457
• Joined: 19-October 09

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:15 AM

cfoley, on 23 July 2010 - 12:13 PM, said:

All you need then is a System.out.println(???????) to display them.

Leave that for him/her to do atleast

### #11 rakesh0

Reputation: -3
• Posts: 11
• Joined: 23-July 10

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:40 AM

actually what m trying to do is..m trying to count the no. of words usig that code but unable to do,,,,,help me
```class words
{
public static void main (String args[])
{
String name="welcome to java";
int l=0;
int i;
int j;
for(i=0;i<name.length;i++)
{
for(j=0;j<=name[i].length()-1;i++)
{
if(name[i].charAt(i)==' ')
{
l++;
}
}
}
System.out.println(+l);
}}
```

Admin Edit: Please use code tags when posting your code. Code tags are used like so =>

Thanks,
PsychoCoder

### #12 eZACKe

• Garbage Collector

Reputation: 120
• Posts: 1,278
• Joined: 01-June 09

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:43 AM

Well why not try what bcranger said? You don't need any loops for this.

This post has been edited by eZACKe: 23 July 2010 - 06:44 AM

### #13 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:44 AM

rakesh0, please use code tags, like so: . Also, please properly format your code so we can read it easier. Why don't you just use bcranger's solution?

### #14 m-e-g-a-z

• Winning

Reputation: 497
• Posts: 1,457
• Joined: 19-October 09

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:45 AM

macosxnerd101, could you merge this thread?

### #15 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

## Re: Find Number of Words in a String

Posted 23 July 2010 - 06:47 AM

They are different topics, as that one is trying to find the number of occurrences of a character in a String. I'm going to keep the topics as they are for now.