CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2008
    Posts
    21

    mirror Strings using recursive method

    Hi,
    I have been tasked to a string recursive problem for my computer science homework. The task is to write a recursive method with the following signature: public String mirror(String str) that returns a copy of the string, followed by all the letters except the last in reverse order. For example, if given "Saturday", you should return "SaturdayadrutaS".

    I have written a java code that does the mirror but I seem to get the complete string together. Hope someone can give me some hints on how to achieve the result using recursion.

    This is the code:
    public String mirror(String str){
    int length = str.length();


    //char secondLastChar = Character.toLowerCase(str.charAt(length-1));

    if(length <= 1){
    return str;
    }

    else{

    char c = str.charAt(length-1);


    return c + mirror(str.substring(0,length-1));


    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: mirror Strings using recursive method

    Are you sure that's exactly what your homework question says?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jan 2008
    Posts
    21

    Re: mirror Strings using recursive method

    Yes. I am sure. Is it possible to achieve that using recursion?

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: mirror Strings using recursive method

    Not if the recursive method has to have the signature shown.

    You can do it if your mirror method can call a method with a different signature which is recursive but why would you even want to do this with recursion? There are much better and much easier ways of achieving the required result.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: mirror Strings using recursive method

    Quote Originally Posted by keang View Post
    Not if the recursive method has to have the signature shown.

    You can do it if your mirror method can call a method with a different signature which is recursive but why would you even want to do this with recursion? There are much better and much easier ways of achieving the required result.
    I think it is possible (and quite easy) with a method with that signature. But I agree with you that there are better solutions without using recursion.

    @raylistic87, your attempt is not too far from a recursive solution, but you are only getting the string reversed. Just remember that each character in the original string (except for the last one) goes twice in the result.

    Have a look at the example you were given:
    Code:
    Saturday => SaturdayadrutaS
    Thinking "recursively", you take the first character (S) and you return that character followed by the mirror of the rest of the string (aturday) followed by the first character again. If the string has only one character just return it (you are doing this already).

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: mirror Strings using recursive method

    Thinking "recursively", you take the first character (S) and you return that character followed by the mirror of the rest of the string (aturday) followed by the first character again. If the string has only one character just return it (you are doing this already).
    Yes you are right, my mistake.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Jan 2008
    Posts
    21

    Re: mirror Strings using recursive method

    Quote Originally Posted by jcaccia View Post
    I think it is possible (and quite easy) with a method with that signature. But I agree with you that there are better solutions without using recursion.

    @raylistic87, your attempt is not too far from a recursive solution, but you are only getting the string reversed. Just remember that each character in the original string (except for the last one) goes twice in the result.

    Have a look at the example you were given:
    Code:
    Saturday => SaturdayadrutaS
    Thinking "recursively", you take the first character (S) and you return that character followed by the mirror of the rest of the string (aturday) followed by the first character again. If the string has only one character just return it (you are doing this already).
    Thanks! I manage to solve it!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured