CSharp - Write program to check whether a string is a palindrome or not.

Requirements

Write program to check whether a string is a palindrome or not.

Input "level", return true

Input "level1", return false

Hint

We will reverse the string and then check whether the original string and the reversed strings are same.

If they are same, the string is a palindrome string.

Demo

using System;

public class MainClass
{
    public static void Main(String[] argv)
    {// www.  j av  a 2s  .  c o m
        string s1 = "abccba";
        char[] tempArray = s1.ToCharArray();
        Array.Reverse(tempArray);
        //change the reverse array to a string and compare
        string reverseStr = new string(tempArray);
        if (s1.Equals(reverseStr))
        {
            Console.WriteLine("String \" {0} \" is a palindrome string, reverse string of it is { 1}", s1, reverseStr);
        }
        else
        {
            Console.WriteLine("String \"{0}\"is a Not palindrome string, reverse string of it is { 1}", s1, reverseStr);
        }
    }
}

Result