| | |
occurence of a word in a text.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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
Views: 1762 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows





