Advertisement
AvengersAssemble

PolyndromString

Feb 7th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1.         public static int Polyndrom(string st)
  2.         {
  3.             string current;
  4.             int m, maxLength = 0;
  5.             for (int i = 0; i < st.Length; i++)
  6.             {
  7.                 current = "";
  8.                 m = 1;
  9.                 if (st[i] == '*')
  10.                 {
  11.                     while (((i - m) >= 0 && (i + m) < st.Length) && (st[i - m] == st[i + m]))
  12.                     {
  13.                         current += st[i - m];
  14.                         m++;
  15.                     }
  16.                     if (current.Length * 2 + 1 > maxLength)
  17.                         maxLength = current.Length * 2 + 1;
  18.                 }
  19.             }
  20.             return maxLength;
  21.         }
  22.         static void Main(string[] args)
  23.         {
  24.             int num;
  25.             string st;
  26.             Console.WriteLine("String: ");
  27.             st = Console.ReadLine();
  28.             num = Polyndrom(st);
  29.             Console.WriteLine("Longest polyndrom with a '*' in the middle: " +num);
  30.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement