Algoritmo para Palindromas o Capicuas en C#


Primera aproximación: Recorriendo toda la cadena.

            Boolean palindroma = true;
            string str = new string(Console.ReadLine().ToCharArray());
            for (int i=0; i < str.Length; i++)
            {
                if (str[i] != str[str.Length-i-1])
                    palindroma = false;
            }
            Console.WriteLine(palindroma.ToString());

Segunda aproximación: Solo evaluamos la mitad de la cadena

            Boolean palindroma = true;
            string str = new string(Console.ReadLine().ToCharArray());
            for (int i=0; i < str.Length; i++)
            {
                if (str[i] != str[str.Length-i-1])
                    palindroma = false;
            }
            Console.WriteLine(palindroma.ToString());

Tercera aproximación: el bucle corta por falso de palindroma.

            string str = new string(Console.ReadLine().ToCharArray());
            for (int i = 0; i < str.Length / 2; i++)
            {
                if (str[i] != str[str.Length - i - 1])
                    return false;
            }
            return true;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s