Results 1 to 5 of 5
- 05-07-2010, 12:52 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
please help with counting specific word in a sentence
how do i count a specific word in a sentence. example:
"i have a dog"
the word "dog" occurred 1 time
here is the code i that i develop so far
i thought that if i count each letter first then i might be able to have some idea on how to count a specific word. please help me thanksJava Code:public class count{ public static void main(String[] args){ String sentence = "this is a book this is a cat this is a dog"; char []abc= new char[26]; int []times=new int [26]; for(int i=0; i<abc.length;i++) abc[i]=(char) (i+97); for(int i=0; i<sentence.length();i++){ char c=sentence.charAt(i); for (int j=0;j<abc.length;j++) if (c==abc[j]) times[j]++; } for(int i=0;i<times.length;i++) System.out.println(abc[i]+" was used "+times[i]+" times"); } }
- 05-07-2010, 01:13 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Suppose that you split the sentence into an array of words ...
- 05-07-2010, 01:27 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
thanks,i'll try to do that
- 05-07-2010, 01:31 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
The String.split method comes to mind here for the splitting into array part.
- 05-07-2010, 02:06 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Counting specific word from a file
By jaq in forum New To JavaReplies: 2Last Post: 12-02-2009, 06:12 PM -
enter a string sentence
By amorosa19 in forum New To JavaReplies: 11Last Post: 01-28-2009, 04:05 AM -
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 10:40 AM -
[SOLVED] Help on Word and Character counting in java
By Alistair in forum New To JavaReplies: 2Last Post: 05-15-2008, 03:48 AM -
How to extract info from a sentence
By luisarca in forum XMLReplies: 1Last Post: 06-07-2007, 05:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks