Songul
Songul

Reputation: 45

finding number of words in a String using recursive method

I want to count number of words in my string using recursive method (java)

so far i wrote this code

public static int CountWords(String sen) {
    int count = 0;
    int i = sen.indexOf(" ");
    if (sen.isEmpty()) {
        return 0;
    }else 
        if (i == sen.indexOf(" ")) {
      return count++;
   }
  //sen.substring(0,sen.indexOf(" ")-1);
    count++;
    return count + CountWords(sen.substring(i + 1));
}

i always get 0 when i call the method can anyone help me make this code run

Upvotes: 1

Views: 3022

Answers (4)

Gagandeep Singh
Gagandeep Singh

Reputation: 19

This method uses a String with no spaces as a base case. Then it removes everything up to and including the first space in the String and recurses.

It handles both the special case of an empty String and the case that a String passed to the method starts with a space appropriately.

public static int CountWords(String sen) 
{   int i = sen.indexOf(" ");
    if(sen.isEmpty()) return 0;          // special case
    if(i == -1) return 1; // base case

    if(i != 0)
        return 1 + CountWords(sen.substring(i+1));
    else
        return CountWords(sen.substring(1));
}

Upvotes: 0

Nathan Hughes
Nathan Hughes

Reputation: 96385

How you're using indexOf is the problem. You're setting i to the result of calling indexOf, then seeing if it's equal to the result of calling indexOf on the same string with the same parameter. The result of the test i == sen.indexOf(" ") will always be true. That's why you always get 0.

String#indexOf returns -1 if the char it's looking for is not found. indexOf comes in very handy here.

Also you shouldn't need a local count variable. Introducing a variable here just makes the code harder to read, because the reader has to hunt around to figure out what the value of it is.

Assuming your input always has exactly one blank between words this could be done as:

public static int countWords(String s) {
    if (s.isEmpty()) return 0;
    if (s.indexOf(" ") == -1) return 1;
    return 1 + countWords(s.substring(s.indexOf(" ") + 1));
}

For multiple blanks between words you can check for a blank and skip past it:

public static int countWords(String s) {
    if (s.isEmpty()) return 0;
    if (s.indexOf(' ') == -1) return 1;
    if (s.charAt(0) == ' ') return countWords(s.substring(1));
    return 1 + countWords(s.substring(s.indexOf(' ') + 1));
}

Upvotes: 3

hellboy
hellboy

Reputation: 2252

This will work -

public static int CountWords(String sen) {
    if("".equals(sen)){
        return 0;
    }
    int count = 0;
    int i = sen.indexOf(" ");
    String substr = sen.substring(0,i+1) ;
    if (i != -1) {
        count++;
    }else{
        if(sen.length()>0){
            count++;
        }
        sen="";
    }
    //sen.substring(0,sen.indexOf(" ")-1);
    return count + CountWords(sen.substring(substr.length()));

}

Upvotes: -3

BadIdeaException
BadIdeaException

Reputation: 2124

This should work, I think:

public static int countWords(String sen) {
    int i = sen.indexOf(" ");
    if (sen.isEmpty()) {
        return 0;
    } else if (i == -1) {
        return 1;
    } else return 1 + countWords(sen.substring(i + 1));
}

Some notes on what is happening:

  1. Java naming conventions dictate you should start method names with a lower case letter
  2. The line if (i == sen.indexOf(" ")) is redunant - you just assigned i to be that before, so it'll always evaluate to true.
  3. And therefore, your recursion never gets called. You need to change it so that if sen isn't empty and contains at least one more space, countWords calls itself with sen minus the first word.

Upvotes: 1

Related Questions