N
N
netrox2016-08-10 18:52:54
recursion
netrox, 2016-08-10 18:52:54

How does this recursive method work?

Explain in detail how recursion works in this program:

using System;
class RevStr
{
    
    public void DisplayRev(string str)
    {
        if (str.Length > 0)
        {
            DisplayRev(str.Substring(1, str.Length - 1));
             
        }
        else
            return;
        Console.Write(str[0]);
    }
}
class RevStrDemo
{
    static void Main()
    {
        string s = "Это тест";
        RevStr rsOb = new RevStr();
        Console.WriteLine("Исходная строка: " + s);
        Console.Write("Перевернутая строка: ");
        rsOb.DisplayRev(s);
        Console.WriteLine();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Moseychuk, 2016-08-10
@fshp

"ABCD" → "BCD" → "CD" → "D" → "" ↘
print A ← print B ← print C ← print D
When using the debugger, everything becomes clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question