Wednesday 19 August 2015

Palindrome Program in c#.net

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

namespace Palindromeprogram
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = string.Empty;
            Console.WriteLine("Enter a String");

            string s = Console.ReadLine();

            int i = s.Length;
            //we can get the Length of string by using Length Property

            for (int j = i - 1; j >= 0; j--)
            {
                str = str + s[j];
            }
            if (str == s)
            {
                Console.WriteLine(s + " is palindrome");
            }
            else
            {
                Console.WriteLine(s + " is not a palindeome");
            }

            Console.WriteLine(str);

            Console.ReadLine();
        }
    }
}

Output:

 1.  Enter a String "allagalla"
        
       allagalla is palindrome
       allagalla

2. Enter a String "India"
         
         India is not a Palindrome
         India

3.  Enter a String "madam"
      
         madam is a Palindrome
         madam

4 comments:

  1. Easy and great code thnx for sharing

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Here is my code to check palindrome string

    public static bool palindrome(string str)
    {

    if (str.Length < 2) return false;

    for (int i = 0, len = str.Length - 1; i < str.Length / 2; i++, len--)
    {
    if (str[i] == '.') i++;// This code is to ignore .
    if (str[len] == '.') len--;//this code is to ignore .
    if (str[i] != str[len])
    return false;
    }
    return true;
    }

    ReplyDelete