找出字符串中所有对称的字符子串

written by Li

 1 namespace ConsoleApplication3
 2 {
 3     class Program
 4     {
 5         public static void findHuiWen(string s)
 6         {
 7             string tmp = null;
 8             for (int i = 0; i < s.Length; i++)
 9             {
10                 for (int j = s.Length - 1; j > i; j--)
11                 {
12                     while (s[i] == s[j])
13                     {
14                         tmp = s.Substring(i, j+1-i);
15                         if (IsHuiWen(tmp))
16                         {
17                             Console.WriteLine(tmp);
18                             break;
19                         }
20                         else
21                         {
22                             break;
23                         }
24                     }
25                 }
26             }
27         }
28 
29 
30         public static bool IsHuiWen(string a)
31         {
32             string sb = null;
33             for (int i = 0; i < a.Length; i++)
34             {
35                 sb += a[a.Length -1 - i];
36             }
37             if (sb == a)
38             {
39                 return true;
40             }
41             else
42             {
43                 return false;
44             }
45         }
46 
47 
48         static void Main(string[] args)
49         {
50             string s = "abaabaccdefggfed";
51             findHuiWen(s);
52         }
53     }
54 }


written by Wang

 1 namespace ConsoleApplication4
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             string testString = "abaabaccdefggfed";
 8             string subString;
 9             string revertString;
10             string result = null;
11 
12             for (int i = 0; i < testString.Length; i++)
13             {
14                 for (int j = 2; j < testString.Length - i; j++)
15                 {
16                     subString = testString.Substring(i, j);
17                     revertString = revert(subString);
18                     if (subString == revertString)
19                     {
20                         result += subString;
21                         result += ";";
22                     }
23                 }
24             }
25             Console.WriteLine(result);
26             Console.Read();
27         }
28 
29         static string revert(string subString)
30         {
31             char[] revertString = subString.ToCharArray();
32 
33             if (revertString.Length > 0)
34             {
35                 for (int i = 0; i < ((revertString.Length) / 2); i++)
36                 {
37                     char temp = revertString[i];
38                     revertString[i] = revertString[subString.Length - 1 - i];
39                     revertString[subString.Length - 1 - i] = temp;
40                 }
41             }
42             string result = new string(revertString);
43             return result;
44         }
45     }
46 }


C#,编译已通过

posted @ 2012-07-02 14:39  云翔紫梦  阅读(926)  评论(0编辑  收藏  举报