Reverse, recursive Learn programming C#

Lesson:

Functions


Exercise:

Reverse, recursive


Objetive:

Create a program that uses recursion to reverse a string of characters (for example, from "Hello" it would return "olleH").


Code:

using System;
public class exercise128
{
    public static string Reverse(string text)
    {
        if (text.Length <= 1)
            return text;

        char firstLetter = text[0];
        string rest = text.Substring(1);


        return Reverse(rest) + firstLetter;
    }

    public static void Main(string[] args)
    {
        Console.WriteLine(Reverse("Juan"));
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.