Palindrome Checking in C#

Introduction

A Palindromic number or string can be read in both directions. It’s a form of word puzzle. A numeral Palindrome is a number which remains the same when its digits are reversed. The term palindromic is derived from Palindrome, which refers to a word whose spelling is unchanged when its letters are reversed.

Sample Application to Check Palindromic Features

To demonstrate Palindromic features, let’s create a .NET console application. Add the following namespaces in the Program.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Following is the source code of the C# CheckPalindrome function to reverse a number and check if it is a Palindrome. The C# program is successfully compiled and executed with Microsoft Visual Studio. It first reverses a number; then, it checks if the given number and reversed numbers are equal. If they are equal, it’s a Palindrome.

static void CheckPalindrome()
{

   int number, remind, sum = 0, temp;
   Console.WriteLine("n >>>> Purpose of this Program <<<< ");
   Console.WriteLine("n >>>> To Find a Number is a Palindrome
      or not <<<< ");
   Console.Write("n Please Enter a number: ");
   number = Convert.ToInt32(Console.ReadLine());
   temp = number;
   while (number > 0)
   {
      remind = number % 10;
      number = number / 10;
      sum = sum * 10 + remind;

   }
   Console.WriteLine("n The Reversed Number of the Original Number
      is: {0} n", sum);
   if (temp == sum)
   {
      Console.WriteLine("n Your Number is a Palindrome nn");
   }
   else
   {
      Console.WriteLine("n Your Number is not a Palindrome nn");
   }
   Console.ReadLine();
   Console.Clear();
}

Similar to Palindrome number checking, we also can check if a string is a Palindrome or not. For that, you first need to reverse the string.

After that, use the equals() method or ‘==’ to match the original string with the reversed version. If the result is true, that would mean the string is a Palindrome.

To check a Palindrome string, I have written the following StringPalindrome function.

static void StringPalindrome()
{
   string s, revs = "";
   Console.WriteLine("n >>>> Purpose of this Program <<<< ");
   Console.WriteLine("n >>>> To Find if a String is a Palindrome
      or not <<<< ");
   Console.WriteLine("n Please Enter a string: ");
   s = Console.ReadLine();
   s = s.Trim();
   for (int i = s.Length - 1; i >= 0; i--)
   {
      revs += s[i].ToString();
   }
   if (revs == s)
   {
      Console.WriteLine("String is a Palindrome n You have Entered
         the Following String {0} n and the reverse string is {1}",
         s, revs);
   }
   else
   {
      Console.WriteLine("String is not a Palindrome n You have
         Entered the Following String {0} n and the reverse string
         is {1}", s, revs);
   }
   Console.ReadKey();
}

Finally, from the Main() method, both the CheckPalindrome and StringPalindrome functions are called.

namespace ProjectPalindrome
{
   class Program
   {
      static void Main(string[] args)
      {
         CheckPalindrome();
         StringPalindrome();
      }
   }
}

Conclusion

In this article, I have coded couple of simple C# methods to test for Palindromes. Many Palindromes are composed of multiple words and have spaces and punctuation. These can all be tested.

I hope you enjoyed reading the article. That’s all for today; happy reading!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read