palindrome function in c# code example
Example 1: c# palidrone
public static void Main(string[] args)
{
Console.WriteLine(new Program().Pal("AsdsA"));
}
public bool Pal(string str)
{
int end = str.Length-1;
for (int i = 0, j = end; i < end; i++, j--)
{
char frontLetter = str[i];
char endLetter = str[j];
if (frontLetter != endLetter) return false;
}
return true;
}
---------------------------------------------------------Option1
public bool Pal(string str)
{
char[] rev = str.ToCharArray();
Array.Reverse(rev);
string reversed = new string(rev);
if (str.Equals(reversed)) return true;
else return false;
}
----------------------------------------------------------Option2
Example 2: palindrom checker C#
static void Main(string[] args)
{
string _inputstr, _reversestr = string.Empty;
Console.Write("Enter a string : ");
_inputstr = Console.ReadLine();
if (_inputstr != null)
{
for (int i = _inputstr.Length - 1; i >= 0; i--)
{
_reversestr += _inputstr[i].ToString();
}
if (_reversestr == _inputstr)
{
Console.WriteLine("String is Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);
}
else
{
Console.WriteLine("String is not Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);
}
}
Console.ReadLine();
}