Wednesday, March 23, 2011

Find the biggest palindrome in the given string

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;
}
}
}
}

No comments: