c# check if palindrome 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();
}
Example 3: check if palindrome recursion in c#
public static bool IsPalindrome(string text)
{
if (text.Length <= 1)
return true;
else
{
if ( text[0] != text[ text.Length - 1 ] )
return false;
else
return IsPalindrome( text.Substring( 1, text.Length-2 ) );
}
}