0

I am trying to get more familiar with recursion in java. I am trying to count number of times character occurs in a given string.

public class apptest {

    public static void main(String[] args) {
        apptest c = new apptest();
        String input = "aaa";
        char p = 'a';

        c.freq(input, p);
    }

    public int freq(String c, char p) {
        if (c.length() == 0) {
            return 0;
        } else if (c.charAt(0) == p) {

            return 1 + freq(c.substring(1, c.length()), p);
        } else
            return freq(c.substring(1, c.length()), p);
    }
}

I am not getting any output. and completely confused on how to solve a problem like this. I looked online and found the freq(c.substring(1, c.length()),p); part but going through the code it doesn't make sense.. seems like on every pass its still going to deal with 'aa' and not necessarily shrink it.. what am I not seeing?

2 Answers 2

4

Your code looks good, but you're not getting output because you're not printing it!

Simply add a System.out.println(...) to your main method.

System.out.println("Frequency is: " + c.freq(input, p));
Sign up to request clarification or add additional context in comments.

2 Comments

LOL OMG THANKS.. i was adding println inside the freq method and I kept seeing multiple println's so I thought it was wrong...BUT THANK YOU
@user2556304: you're welcome! Were all problems so easy to solve. :)
1

For this part:

I looked online and found the freq(c.substring(1, c.length()),p); part but going through the code it doesn't make sense.. seems like on every pass its still going to deal with 'aa' and not necessarily shrink it.

The line c.substring(1, c.length()) shrinks the String c so that what gets passed into the recursive call has one less character to process and thus, helping the recursive call to eventually reach the termination condition of c.length() == 0. So, it is safe to assume that freq method's implementation is correct.

14 Comments

@Downvoters: please, read through the question before voting. OP asked two questions, and since Hover already answered one I won't repeat that.
I'll 1+ since you're answering the secondary question that I missed.
@HovercraftFullOfEels thanks, that's one for sportsmanship badge:) I've got 3 -1's but the voters all removed those. +1 for you too.
Your answer does not make sense. Whats the use of recursion if input is not reducing with every call. Read your answer and see if it make any sense. If he uses same string for each recursive call, then, when it will terminate...let me guess ...with "StackOverflow"?
@WandMaker Who said an answer must solve a problem in the code? This answer replies to the question OP asked which is not "how" but is "why". Downvote if you want to, I simply have no right nor method to stop you, but I still think you misunderstood. BTW, perhaps my English is bad, but I did not intend to hint that there is a problem in the code.
|

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.