- Course
- Functions C
- Function to check palindrome
Function to check palindrome
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that implements a function to check if a string is a palindrome or not. A palindrome are strings that can be read equally from right to left, such as 'radar'.
Input
radar
Output
True
Solution
using System;
public class CheckPalindrome
{
public static void Main(string[] args)
{
string text = Console.ReadLine();
Console.WriteLine(IsPalindrome(text));
}
public static bool IsPalindrome(string text)
{
text = text.ToUpper();
int j = text.Length - 1;
for (int i = 0; i < j; i++)
{
if (text[i] != text[j])
{
return false;
}
j--;
}
return true;
}
}