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.

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.

Recommended Answers

All 6 Replies

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..

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);
    }
 }

thanks tyagi it works.

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

Look at an ASCII value table. You will get your answer.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.