user2920249
user2920249

Reputation: 137

Write a recursive method that returns the number of occurences of 'A' in the string it is passed

So I've been fiddling with this problem for the past hour. I keep getting unexpected type error. It appears to be from a confliction between charAt and s.length. Any ideas on what could fix that?

class lab7
{
    public static void main(String[] args)
    {
        String s = ("BCA");
    }

    public static String recursion(String s)
    {
        if (s.length()>=0)
        {
            if(s.charAt(s.length()) = A)
            {
                count++;
            }
            s.substring(0, s.length()-1);
        }
        return count;
    }
}

Upvotes: 0

Views: 1245

Answers (3)

user1019830
user1019830

Reputation:

Consider this snippet:

static int countA(String str) {
    if (str == null || str.length() == 0) {  /* nothing or "" contains 0 A's */
        return 0;
    }
    return (str.charAt(0) == 'A' ? 1 : 0 )   /* 0 or 1 A's in first character */
                + countA(str.substring(1));  /* plus no. of A's in the rest */
}

And you call the function like this:

int a = countA("ABAABA");  /* a is 4 */

I realize now that this question was school related, but at least this snippet works as an exercise in understanding recursion.

Upvotes: 1

KodingKid
KodingKid

Reputation: 971

Following code uses String class. For performance critical applications you might want to use StringBuffer / StringBuilder class accordingly.

class StringCounter
{
    public static void main (String[] args) 
    {
        int count = returnCount("ABCDABCDABCD", 0);
        System.out.println(count);
    }
    public static int returnCount(String s, int count)
    {
        // You may want to do some validations here.
        if(s.length()==0)
        {
            return count;
        }
        if(s.charAt(0)=='A')
        {
            return returnCount(s.substring(1), count+1);            
        }
        else
        {
            return returnCount(s.substring(1), count);
        }
    }
}

The code simply slices the String parameter one character at a time and checks for the required character. Further on every invoke it will update the count and String parameter.

Any ideas on what could fix that?

  1. Your function is not recursive. Recursive functions call themselves with manipulated/updated parameters.
  2. As a thumb rule in recursive functions, always think in terms of manipulating function parameters.
  3. Always have a base case that will terminate recursive calls.

Upvotes: 1

templatetypedef
templatetypedef

Reputation: 372724

There are several issues with this code, including some significant logic errors. However, the specific error you're getting is probably here:

if(s.charAt(s.length()) = A)

First, note that you're using = instead of ==, which does an assignment rather than a comparison. Also note that A should be in single quotes to be a character literal. Right now, Java thinks A is the name of a variable, which isn't defined. Finally, note that strings are zero-indexed, so looking up the character at position s.length() will give you a bounds error.

I hope this helps you get started! As a hint, although your function is named "recursion," does it actually use recursion?

Upvotes: 1

Related Questions