check a given string is palindrome or not in C#.NET

 How to check a given string is palindrome or not in C#.NET?

check string palindrome or not

Fig.1 String Palindrome



Program Steps

  1. Get a string
  2. Reverse it.
  3. Compare the original string with Reverse string.
  4. If True then Palindrome other wise not a palindrome.

Input

INDIA


Output

INDIA

AIDNI

STRING IS NOT PALINDROME


Program

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace CsharpProgram
{
    class Program
    {       
        public static void Main(string[] args)
        {           
       
            ReverseString("INDIA");
        }    
        private static void ReverseString(string str)
        {
            StringBuilder sb=new StringBuilder();
            Console.WriteLine("Before String Reverse....." + str);

            //Loop right to left
            
            for(int i=str.Length-1;i>=0;i--)
            {
                sb.Append(str[i]);
            }
 Console.WriteLine("After String Reverse......" + sb.ToString());
            
            //Compare two string to check palindrome

            if(str==sb.ToString())
                Console.WriteLine("String is Palindrome");
                else
                Console.WriteLine("String is not Palindrome");
        }

    }
}

OUTPUT

Before String Reverse.....INDIA
After String Reverse......AIDNI
String is not Palindrome



Share:

Contact for Azure Training

Name

Email *

Message *

Subscribe YouTube

Total Pageviews

Popular Posts

Labels

Recent Posts