Preetam Ramdhave
write a program to find the biggest palindrome in the given string
Posted by Preetam Ramdhave in .Net | ASP.NET on Apr 11, 2011
0
Do you know the answer for this question? Post it below.
Guest

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

namespace Palindrom
{
    class Program
    {
        static void Main(string[] args)
        {
            string string1 = string.Empty;
            string SubstringPali = string.Empty;
            string Palindrom= string.Empty;
            int Count=0;
            int StartIndex;
            int NextCount;
            string GetPalindrom = string.Empty; ;


            string1 = "asdadadasdiac aadaa erereraaggacaec";
            StartIndex = 0;
            foreach (char c in string1)
            {
                StartIndex++;
                if (StartIndex <= string1.Length - 1)
                {
                    if (string1.Substring(StartIndex).Contains(c))
                    {

                        SubstringPali = string1.Substring(StartIndex);
                        foreach (char cu in SubstringPali)
                        {
                            GetPalindrom += cu;
                            if (c == cu)
                            {
                                GetPalindrom = c + GetPalindrom;
                                if (CheckPalindrom(GetPalindrom))
                                {
                                    NextCount = GetPalindrom.Length;
                                    if (Count < NextCount)                                    {
                                        Count = NextCount;                                       
                                    }//if (Count < NextCount)                                  
                                }// if (CheckPalindrom(GetPalindrom))
                                GetPalindrom = "";
                               
                                break;
                            }//if (c = cu)
                        }// foreach (char cu in SubstringPali)
                  
                    }// if (string1.Substring(StartIndex).Contains(c))
                }//(StartIndex <= string1.Length - 1)

            }// foreach (char c in string1)
            Console.Write(" The max count of the palindrom is ={0}", Count);
            Console.Read();
        }

        static bool CheckPalindrom(string Palindrom)
        {
            int Counter;
            string RevPalindrom=string.Empty;
            if (Palindrom.Length > 1)
            {
                for (Counter = Palindrom.Length - 1; Counter >= 0; Counter--)
                {
                    RevPalindrom += Palindrom[Counter];
                }
            }
            if (RevPalindrom == Palindrom)
            {

                return true;
            }
            else
            {
                return false;
            }

        }
    }

}


 

Posted by Preetam Ramdhave on Apr 11, 2011
Sponsored by
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Sponsored by
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PRIVACY POLICY | TERMS & CONDITIONS | SITEMAP | CONTACT US | REPORT ABUSE © 2011 C# Corner. All contents are copyright of their authors.