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,,,,
Find Number of Words in a Stringi want to calculate number of words in a string say-"you are write
30 Replies - 10831 Views - Last Post: 24 July 2010 - 09:43 AM
Replies To: Find Number of Words in a String
#2
Re: Find Number of Words in a String
Posted 23 July 2010 - 05:14 AM
please give me some code for that purpose,,,,,,
#3
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:
And count the length of the array returned by that method:
String[] words = sentence.split(" ");
//words.length is your # of words
#4
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
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]
[rules][/rules]
#6
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
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.
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
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
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
Re: Find Number of Words in a String
Posted 23 July 2010 - 06:15 AM
#11
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
Admin Edit: Please use code tags when posting your code. Code tags are used like so =>
Thanks,
PsychoCoder
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
#13
Re: Find Number of Words in a String
Posted 23 July 2010 - 06:44 AM
#14
Re: Find Number of Words in a String
Posted 23 July 2010 - 06:45 AM
#15
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.
|
|

New Topic/Question
Reply




MultiQuote










|