Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please help me, java recursion problem

    ok so i got into high school AP CPS and am currently learning java on eimacs.com, but there program is not all too help full when learning, at least for me any ways
    i was hoping someone could explain some code to me, what it does etc, and then help me with a problem

    for first the explanation
    Code:
    public static String reverseString( String s )
    {
      if ( s.length() == 0 )
        return "";
     
      String firstChar = s.substring( 0, 1 );
      String reverseRest = reverseString( s.substring( 2 ) );
     
      String result = reverseRest + firstChar;
     
      return result;
    }
     
    public static void main( String[] args ) 
    {
      String a = "The sky's the limit!";
      System.out.println( reverseString( a ) );
    }



    and then the problem i am trying to do is count vowels in a word, using recursion and not using for or while loops
    my code is
    Code:
    
    
    public static int countVowels( String s )
    {
    int i = 0;
    int total = 0;
     
    String vowel = s.substring( i,i+1 );
     
    if (s.equals("") )
        return 0;
        
    if ( vowel == "a" ||  vowel == "A" ||  vowel == "e" ||  vowel == "E" ||  vowel == "i" ||  vowel == "I" ||  vowel == "o" ||  vowel == "O" ||  vowel == "u" ||  vowel == "U" )
     total++;
     i++;
    
     if ( i  <= s.length () )
     {
     i++;
      countVowels ( s.substring ( i, i + 1 ) );
     }
      else 
     return total ;
    }
    which always gives me a missing returns statement, or when i play with the code a stack overflow

    any help please, i am so far behind in my class, and am so lost...

    THANK YOU IN ADVANCE,
    RATHRATHON
    Last edited by Fou-Lu; 11-14-2011 at 04:58 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Recursion always works the same. Keep going until you find a satisfied result, otherwise, try again. With the first example, the check is on the length of the provided string, once it hits 0 there is no more recursion. Examples are hard to do though, so you need to follow a stack trace. With just the word 'The', you would end up with:
    Code:
    firstChar1 = 'T'
    SUB
    firstChar2 = 'e';
    SUB
    NULL
    So the end result would be 'eT'. The answer appears wrong since the recursion only works from the third character on. That should be fetching from substring(1).
    You never need to work with an increment when dealing with recursion. This is because the parameter should be modified to match only the remaining options. So with a vowel counter you would need
    Code:
    SUB COUNTVOWELS(STRING input)
        SET iTotal TO 0
        IF input.LENGTH > 0 THEN
            SET sEvaluate TO input.SUBSTRING(0, 1)
            IF sEvaluate.ISVOWEL() THEN
                SET iTotal TO 1
            ENDIF
        ENDIF
        SET iTotal TO iTotal + (CALL COUNTVOWELS(input.SUBSTRING(1)))
        RETURN iTotal
    END SUB
    ISVOWEL can actually be done from either the Character class, or by comparing. This is wrong though: if ( vowel == "a" || vowel == "A". Strings cannot be compared this way. If you stick to string, you need to use vowel.equals("a") or vowel.equalsIgnoreCase("a").

    Edit:
    Sorry, the above is pseudo code. Please implement it and come back if it gives you any trouble.
    Last edited by Fou-Lu; 11-14-2011 at 05:40 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •