C# Sharp Exercises: Check whether a string is Palindrome or not
C# Sharp Recursion: Exercise-8 with Solution
Write a program in C# Sharp to check whether a given string is Palindrome or not using recursion.
Pictorial Presentation:
Sample Solution:
C# Sharp Code:
using System;
public class RecExercise8
{
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 ) );
}
}
public static void Main()
{
Console.Write("\n\n Recursion : Check whether a string ia Palindrome or not :\n");
Console.Write("---------------------------------------------------------------\n");
string str1;
bool tf;
Console.Write(" Input a string : ");
str1 = Console.ReadLine();
tf=IsPalindrome(str1);
if (tf==true)
{
Console.WriteLine(" The string is Palindrome.\n");
}
else
{
Console.WriteLine(" The string is not a Palindrome.\n");
}
}
}
Sample Output:
Recursion : Check whether a string ia Palindrome or not : --------------------------------------------------------------- Input a string : eye The string is Palindrome.
Flowchart :
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a program in C# Sharp to check whether a number is prime or not using recursion.
Next: Write a program in C# Sharp to find the factorial of a given number using recursion.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework