ideas | discover | explore

Saturday, January 12, 2013

C# 18 – String Manipulation – Creating a Palindrome Checker


---------------------------------------------------------------------------------------------------------------------------
Manage and share your stuff!
or
---------------------------------------------------------------------------------------------------------------------------


In this tutorial we will be doing some string manipulation by creating a program called Palindrome Checker that will determine if a word or a string of letters is a palindrome.

Before we proceed we need to know what is the meaning of a palindrome.

What is a palindrome? According to Wikipedia:
“A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, with general allowances for adjustments to punctuation and word dividers.”

Which means a word is read the same whether it’s in normal order or in reversed order.
e.g. 
kayak
nissin
madam

Ok let’s begin.

1.     Create a new console project.
2.     Add the following code inside of class Program.

static void Main(string[] args)
       {
            string input;

            Console.WriteLine("Welcome to Palindrome Checker");
            Console.WriteLine("- The software will check if your input is a palindrome. -");
            Console.WriteLine("");
            Console.Write("Enter a string: ");
            input = Console.ReadLine();

            if (IsPalindrome(input))           
                Console.WriteLine(input + " is a palindrome.");
            else
                Console.WriteLine(input + " is a not palindrome.");

            Console.WriteLine("");
            Console.WriteLine("Press any key to exit.");

            Console.ReadLine();
        }

---------------------------------------------------------------------------------------------------------------------------
Manage and share your stuff!
or
---------------------------------------------------------------------------------------------------------------------------


        private static bool IsPalindrome(string input)
        {
            string reverse = "";

            for (int i = input.Length-1; i >= 0; i--)
            {
                reverse = reverse + input[i];
            }
           

            if (input.Equals(reverse))
                return true;

            return false;
        }

3.     Press F5 to run the program. You output should be something similar below.


You have noticed in the output above that I entered the word “kayak” and pressed Enter. After that the result is shown which is “kayak is a palindrome” in this example.

What we will try to focus our attention now is for this function.

private static bool IsPalindrome(string input)
{
string reverse = "";

       for (int i = input.Length-1; i >= 0; i--)
       {
              reverse = reverse + input[i];
       }
            
       if (input.Equals(reverse))
              return true;

return false;
}

This is the main function that checks if a given string is a palindrome. It creates a variable ‘reverse’ that will hold the reversed string. It enters a loop which starts at the end of the string and then moves backwards one character at a time and stores that character to the ‘reverse’ variable.

After the reversal is complete the next step is to check whether the ‘input’ string is indeed equal to the ‘reverse’ string. If they are equal then the function should return true or false if its not.

Hope you enjoyed our simple and quick tutorial.

Be happy and keep coding!

---------------------------------------------------------------------------------------------------------------------------
Manage and share your stuff!
or
---------------------------------------------------------------------------------------------------------------------------

No comments: