Wednesday, June 11, 2014

Widgets

Code to Check whether Given Number or Sentence is Palindrome

Today i will tell you program in C#, about how to check whether a string is a Palindrome or not. You may also check Short - Three Statement long version.

What is Palindrome?
Palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed. For example: "madam", "1221". 

Here's the code:

using System;
namespace Palindrome_Checker
{
class Program
{
    static void Main(string[] args)
    {
        PalindromeChecker_class palindrome = new PalindromeChecker_class();
        Console.WriteLine("Enter phrase to be checked:");
        switch (palindrome.PalindromeChecker((Console.ReadLine())))
        {
            case true:
                {
                    Console.WriteLine("Yes, this phrase is Palindromic");
                    break;
                }
            case false:
                {
                    Console.WriteLine("No, this phrase is not Palindromic");
                    break;
                }
        }
        Console.ReadLine();
    }
}
class PalindromeChecker_class
{
    char[] phrase;
    bool ispalindrome = true;
    public bool PalindromeChecker(string phrase_to_be_checked)
    {
        phrase = phrase_to_be_checked.ToString().ToCharArray();
        switch (phrase_to_be_checked.Length)
        {
            case 1:
                {
                    return true;
                }
            case 2:
                {
                    return (phrase[0] == phrase[1]);
                }
            case 3:
                {
                    return (phrase[0] == phrase[2]);
                }
            default:
                {
                    for (int j = 0; ispalindrome && j < phrase.Length / 2; j++)
                    {
                        ispalindrome = (phrase[j] == phrase[phrase.Length - (j + 1)]);
                    }
                    return ispalindrome;
                }
        }
    }
}
 
}Some of the features of this Program are:
1. Input can be anything: a number, special characters, sentence in English or any other language.
2. It can check number and sentence of such a big length that when we assign that number or sentence to an array, the size of array does not exceed 2GB. (Since in .NET 4.5 "No object can have size greater than 2GB).

Hope you like it!

M.S. Saggoo is a Software Engineer and founder of , a popular  programming news and development blog since 2013.
What can I do for you? I can be found on  and can be reached by email .

No comments:

Post a Comment