列出一个字符串中所有对称的字串,如输入:“a^_^aXyZZzy123321",期望:“a^_^a","^_^","ZZ","33","2332",  代码不会了。拜托高手代码和思路

解决方案 »

  1.   


            static void Main(string[] args)
            {
                string a = "a^_^aXyZZzy123321";            string[] subs = GetSubString(a);
                foreach (string subString in subs)
                {
                    if (subString == new string(subString.ToCharArray().Reverse().ToArray()))
                        Console.WriteLine(subString);
                }
            }        public static string[] GetSubString(string s)
            {
                List<string> list = new List<string>();            for (int length = 2; length <= s.Length; length++)
                {
                    for (int index = 0; index + length <= s.Length; index++)
                        list.Add(s.Substring(index, length));
                }            return list.ToArray();
            }
    哈哈,闲来无聊,帮楼主写了个
      

  2.   

    我也来一个 static void Main(string[] args)
            {
                string str = "a^_^aXyZZzy123321";            for (int iStart = 0; iStart < str.Length - 1;  iStart++)
                {
                    char head = str[iStart];//假设为头
                    int iFind = str.IndexOf(head, iStart + 1);//找尾
                    if (iFind > 0)
                    {
                        string strSub = str.Substring(iStart, iFind - iStart + 1);
                        if (check(strSub))
                        {
                            Console.WriteLine(strSub);
                            continue;
                        }
                    }
                }
                Console.Read();
                 }        static bool check(string str)
            {
                for (int i = 0, j = str.Length - 1; i < j; i++, j--)
                {
                    if (str[j] != str[i])
                    {
                        return false;
                    }
                }
                return true;
            }
      

  3.   

     public ArrayList GetValues(string s)
            { 
                ArrayList al = new ArrayList();
                for (int i = 0; i < s.Length; i++)
                {
                    for (int j = 2; j < s.Length - i; j++)
                    {
                       //将对称值添加
                        if(GetResul(s.Substring(i, j)))
                        {
                            al.Add(s.Substring(i, j));
                        }
                        else
                        {                    }
                    }
                }
                return al;
            }
            //判断是否对称
            public bool GetResul(string s)
            {
                bool res = false;
                if (s.Length % 2 == 0)
                {
                    for (int i = 0; i < s.Length / 2; i++)
                    {
                        if (s.Substring(i, 1) == s.Substring(s.Length-1 - i, 1))
                        {
                            res = true;
                        }
                        else
                        {
                            res = false;
                        }
                        if (res == false)
                        {
                            return false;
                        }
                    }
                    return res;
                }
                else
                {
                    for (int i = 0; i < s.Length / 2+1; i++)
                    {
                        if (s.Substring(i, 1) == s.Substring(s.Length-1 - i, 1))
                        {
                            res = true;
                        }
                        else
                        {
                            res = false;
                        }
                        if (res == false)
                        {
                            return false;
                        }
                    }
                    return res;
                }
            }