- Course
- Recursion
- Check palindrome recursively
Check palindrome recursively
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that implements a recursive 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 CheckPalindromeRecursively
{
public static void Main(string[] args)
{
string text = Console.ReadLine();
Console.WriteLine(IsPalindrome(text));
}
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));
}
}
}
}