Palíndromo recursivo Aprende programación con ejercicios C# Sharp

Lección:

Funciones


Ejercicio:

Palíndromo recursivo 46


Objetivo:

Cree una función recursiva para decir si una cadena es simétrica (un palíndromo). Por ejemplo, "RADAR" es un palíndromo.


Código:

using System;
public class exercise131
{
    public static bool IsPalindrome(string text)
    {
        if (text.Length <= 1)
            return true;
        else
        {
            if (text[0] != text[text.Length - 1])
                return false;
            else
                return IsPalindrome(text.Substring(1, text.Length - 2));
        }
    }

    public static void Main()
    {
        Console.WriteLine(IsPalindrome("radar"));
        Console.WriteLine(IsPalindrome("pato"));
    }
}