occurence of a word in a text.

Reply

Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

occurence of a word in a text.

 
0
  #1
Jul 31st, 2008
i was trying to write a code for counting the number of ocurence of a particular word from a sentence.it gives me a wrong output it starts checking for the same alphabets in the sentence rather than the word.

  1. import java.lang.String;
  2.  
  3. public class StringCount2
  4. {
  5. public static void main(String args[])
  6. {
  7. String searchFor = "is";
  8. String base = "This is object oriented programming language ";
  9. int len = searchFor.length();
  10. //System.out.println( "length of object =" +len);
  11. int result = 0;
  12. if (len > 0)
  13. {
  14. int start = base.indexOf(searchFor);
  15. // System.out.println("index of object =" + start);
  16. while (start != -1)
  17. {
  18. result++;
  19. start = base.indexOf(searchFor, start+len);
  20.  
  21. }
  22. }
  23. System.out.println(result);
  24. }
  25. }

can any one help me with this.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: occurence of a word in a text.

 
0
  #2
Jul 31st, 2008
If you need to search for a particular word, use the Regex API --

http://java.sun.com/javase/6/docs/ap...x/Pattern.html

-- though, be warned. It is not fond of the light-hearted occurrence-searcher.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: tyagi is an unknown quantity at this point 
Solved Threads: 0
tyagi tyagi is offline Offline
Newbie Poster

Re: occurence of a word in a text.

 
0
  #3
Jul 31st, 2008
Condition on incrementing the result must do the trick.
Count should increment only if the word you are searching for in the String(Sentence) does not have any letters of Alphabet before or after it.

Try out this..

  1. public class StringCount2
  2. {
  3. public static void main(String args[])
  4. {
  5. String searchFor = "is";
  6. String base = "This is object oriented programming language.is is my favourite.";
  7. int len = searchFor.length();
  8. int result = 0;
  9. if (len > 0)
  10. {
  11. int start = base.indexOf(searchFor);
  12. while (start != -1)
  13. {
  14. int a=(int)(base.charAt(start-1));//Gives the ASCII equivalent of the character before the search String.
  15. int b=(int)(base.charAt(start+searchFor.length()));//Gives the ASCII equivalent of the character after the search String.
  16. //Check if the Character before or after (or both) the search String(*searchFor) is an Alphabet or not.
  17. if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122)))
  18. {
  19. //Not to be incremented as string searchFor is a part of a base.
  20. }
  21. else
  22. result++;//Update the result.
  23. start = base.indexOf(searchFor, (start+len));
  24. }
  25. }
  26. System.out.println(result);
  27. }
  28. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

Re: occurence of a word in a text.

 
0
  #4
Jul 31st, 2008
thanks tyagi it works.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

Re: occurence of a word in a text.

 
0
  #5
Jul 31st, 2008
hi thyagi, i could not understand the if part

if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122)))
how you got those values for and b
pl. clarify
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: occurence of a word in a text.

 
0
  #6
Aug 1st, 2008
Look at an ASCII value table. You will get your answer.
My blog on .NET- http://dotnet.tekyt.info
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: tyagi is an unknown quantity at this point 
Solved Threads: 0
tyagi tyagi is offline Offline
Newbie Poster

Re: occurence of a word in a text.

 
0
  #7
Aug 1st, 2008
Yes.The if part is to check whether the ASCII equivalent of the character adjacent the required word in the string on either sides is a letter of the Alphabet or not. Note that it is 'OR' so as to account for non increment in cases like 'this' in the example you have mentioned, which implies that the ASCII equivalent of the character adjacent the required word in the string falls in the range of that of [a-z] or [A-Z] and hence the variable 'result' is not to be incremented.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC