| | |
occurence of a word in a text.
![]() |
•
•
Join Date: Jul 2008
Posts: 28
Reputation:
Solved Threads: 0
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.
can any one help me with this.
Java Syntax (Toggle Plain Text)
import java.lang.String; public class StringCount2 { public static void main(String args[]) { String searchFor = "is"; String base = "This is object oriented programming language "; int len = searchFor.length(); //System.out.println( "length of object =" +len); int result = 0; if (len > 0) { int start = base.indexOf(searchFor); // System.out.println("index of object =" + start); while (start != -1) { result++; start = base.indexOf(searchFor, start+len); } } System.out.println(result); } }
can any one help me with this.
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.
http://java.sun.com/javase/6/docs/ap...x/Pattern.html
-- though, be warned. It is not fond of the light-hearted occurrence-searcher.
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Solved Threads: 0
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..
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..
Java Syntax (Toggle Plain Text)
public class StringCount2 { public static void main(String args[]) { String searchFor = "is"; String base = "This is object oriented programming language.is is my favourite."; int len = searchFor.length(); int result = 0; if (len > 0) { int start = base.indexOf(searchFor); while (start != -1) { int a=(int)(base.charAt(start-1));//Gives the ASCII equivalent of the character before the search String. int b=(int)(base.charAt(start+searchFor.length()));//Gives the ASCII equivalent of the character after the search String. //Check if the Character before or after (or both) the search String(*searchFor) is an Alphabet or not. if(((a>=65 && a<=90) || (a>=97 && a<=122)) || ((b>=65 && b<=90) || (b>=97 && b>=122))) { //Not to be incremented as string searchFor is a part of a base. } else result++;//Update the result. start = base.indexOf(searchFor, (start+len)); } } System.out.println(result); } }
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
![]() |
Similar Threads
- help for writing a program that'll read in a line of text (C)
- Word Frequency Counter Help (Java)
- read no of occurence of word in a pdf using c# (C#)
- Reading pascal file and searching for a particular word in that file (Pascal and Delphi)
- trouble with counting letter from file, please help. (C++)
Other Threads in the Java Forum
- Previous Thread: Do some manipulations in java?
- Next Thread: Make a 2D Array from ArrayList
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php print problem program programming project recursion repositories scanner scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing swt text-file thread threads tree windows





