0

I've got a nice riddle in C# (I'm kind of starter). I need to recursively reserve a string (within a method). I've tried:

 public static void ReverseString(string str)
    {
        if(str.Length > 0)
        {
            char ch = str[str.Length-1];
            ReverseString(str.Substring(0,str.Length-2));
            Console.Write(ch);
        }
    }

But it doesn't work. I'm allowed to change only the text in the 2 first lines of the if. (the str[str.Length-1] and str.Substring(0,str.Length-2))

What's my mistake? thanks

8
  • Always explain "it doesn't work". Does it print out the wrong values? Does it throw an exception? Does it not compile? What? Commented Sep 9, 2013 at 20:09
  • I think the problem is trying to recursively reverse a string, you don't need recursion at all for this task. Commented Sep 9, 2013 at 20:10
  • 3
    @Matthew It was already explained that it's an academic exercise. There's nothing wrong with that. Commented Sep 9, 2013 at 20:10
  • When trying with a string of 3 ch or less it jumps out of the bound of the array. otherwise, wrong print. Commented Sep 9, 2013 at 20:10
  • 1
    @Asad The constraints of the program prohibit that. Commented Sep 9, 2013 at 20:12

2 Answers 2

9
public static void ReverseString(string str)
{
    if(!String.IsNullOrEmpty(str))
    {
        char ch = str[0];
        ReverseString(str.Substring(1));
        Console.Write(ch);
    }
}

To explain what's going on, the inner most call of Console.Write gets executed first, which because of recursion ends up being the end of the string. Then when the stack starts closing in, it prints the earlier characters.

Sign up to request clarification or add additional context in comments.

3 Comments

Cool, thanks, I'll tick this as the ans when the site allows me.
I didn't see your edit before posting mine, +1. I'll leave my answer up just to keep a different expanation.
@Cemafor, your answer helped me understanding this one a bit deeper, so thanks a lot.
2

It looks like this function is supose to print the string in reverse order to the console. When working with recursion, you should start by assuming the function does what it is supose to do and code around that. Makeing sure any recursive call is done with a smaller data set. In this case, a shorter string.

public static void ReverseString(string str)    // example str="cat"
{
    if(str.Length > 0)
    {
        // grabs the last charactor        "t"
        char ch = str[str.Length-1]; 

        // prints the first n-1 charactors in reverse order   "ac"
        ReverseString(str.Substring(0,str.Length-2));

        // prints that last charactor      "t" leads to "act"... not quite right
        Console.Write(ch);
    }
}

If you are not allowed (for the sake of the exersise) to change the last line, you could try this.

public static void ReverseString(string str)    // example str="cat"
{
    if(str.Length > 0)
    {
        // grabs the first charactor        "c"
        char ch = str[0]; 

        // prints the last n-1 charactors in reverse order   "ta"
        ReverseString(str.Substring(1));

        // prints that last charactor      "c" leads to "tac"... Yeay!!
        Console.Write(ch);
    }
}

Comments

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.