w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Check whether a string ia Palindrome or not

Secondary Nav

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.
Test Data :
Input a string : RADAR
Expected Output
:
The string is Palindrome.

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");
      }
    }
}

C# Sharp Practice online:


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");
      }
    }
}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number