1

My homework is to make a recursive method to count the appearances of a given letter in a given string. Here is my code so far:

import java.util.Scanner;
public class Exercise18_10 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);    
    System.out.print("Enter a string: ");
    String str = sc.next();
    System.out.print("Enter a character: ");
    String letter = sc.next();
    char a = letter.charAt(0);
    System.out.println("The count of " + a + " is: " + count(str, a));

    }
    public static int count(String str, char a) {
        int count = str.indexOf(a);
        return count;
    }
}

In count, I use indexOf to find the first occurrence of the desired letter, but I'm not sure what to do after that.

9
  • 1
    Do you know what indexOf(...) does? Commented May 18, 2017 at 21:54
  • 1
    yes it finds the first occurrence of the specified character Commented May 18, 2017 at 21:55
  • 2
    Also, do you know what recursion is? Because I see none. Commented May 18, 2017 at 21:55
  • 2
    That is why I asked for help, I'm not sure how to make a recursive method out of this Commented May 18, 2017 at 21:56
  • 1
    at indexOf(a, 100);? Commented May 18, 2017 at 21:59

2 Answers 2

1

Your count variable is the position of the first occurrence in the string. Instead, you need something like

public static int count(String str, char a) {
    int exist = str.indexOf(a);
    if ( <a doesn't exist in str*> )
        return 0;
    else {   /* recur on the rest of the string; add 1 */
        rest = str.substr(exist+1, <end of string>)
        return count(rest, a) + 1
    }
}

I've left a lot of this for you to code, but those are the two basic steps:

  • Base case: the character isn't there, so return 0
  • Recursion: count one sighting, add whatever is in the rest of the string.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this makes so much sense now!
0

As you stated, 'count' function currently returns the index of first occurrence of an inputted letter. The idea of counting the number of occurrences recursively would mean you need to break the string down into smaller and smaller parts as you move forward.

First, you look at the whole string, then only the substring of the current string beginning with the next occurrence and so on. To implement recursion here, you might want to create a method which can call itself repeatedly (like Inception!) with a base condition to break out once you're done (i.e. reaching the end of a string or finding no more occurrences of the character in whatever portion of the string you have left --> indexOf(...) == -1)

1 Comment

Thank you for your response this is very helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.