Sunday, 15 December 2013

C# program to test if string is Palindrome or not


In this article we will discuss with you the program for testing a string whether it’s palindrome or not using console application in c#.net:
Palindrome: - Palindrome is nothing but the string that remains the same when its reversed.
Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Declare 2 variable “str_Orginal_String” and “str_Reverse_String” for storing the original   and reverse string and initialize the reverse variable “str_Reverse_String” with blank.
Step3: Read an input from the User in this Variable “str_Orginal_String” for storing that string which you want to test for palindrome.
Step4: now loop through the last position to the first position of that Original string and store each character value in “str_Reverse_String” to get the final Reverse string.

Step5: Now compare the original string with the reverse string if that is same then it would be palindrome string otherwise it will be not palindrome string.

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

namespace Palindrome
{
    class Program
    {

        static void Main()
        {
           
            string str_Orginal_String, str_Reverse_String;
            str_Reverse_String = "";
            Console.WriteLine ("Please enter the string to test for palindrome string ?");

            str_Orginal_String = Console.ReadLine ();

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

            if (str_Orginal_String == str_Reverse_String)
            {
                Console.WriteLine ("The string " + str_Orginal_String + " is palindrome");
            }
            else
            {
                Console.WriteLine ("The string " +str_Orginal_String + " is not                   palindrome");
            }
            Console.ReadLine ();
       }
    }



}

Output:-


No comments:

Post a Comment