Question - Write a program in C# Sharp to find if a given string is palindrome or not?
Answer -
internal static void chkPalindrome(string str)
{
bool flag = false;
for (int i = 0, j = str.Length - 1; i < str.Length / 2; i++, j--)
{
if (str[i] != str[j])
{
flag = false;
break;
}
else
flag = true;
}
if (flag)
{
Console.WriteLine("Palindrome");
}
else
Console.WriteLine("Not Palindrome");
Output:
Input: Key Output: Not Palindrome
Input: step on no pets Output: Palindrome