How to check a given string is palindrome or not in C#.NET?
Fig.1 String Palindrome |
Program Steps
- Get a string
- Reverse it.
- Compare the original string with Reverse string.
- 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