求助。。。对称字符串
列出一个字符串中所有对称的字串,如输入:“a^_^aXyZZzy123321",期望:“a^_^a","^_^","ZZ","33","2332",代码不会了。拜托高手代码和思路 --------------------编程问答-------------------- 在线等 --------------------编程问答-------------------- 递归,从2开始吧,每次去2个,判断是否相等,然后顺序取2个
2个的取完了,取3个,判断第一和第三个字符是否相等
以此类推 --------------------编程问答-------------------- 先写个判断字符串是否是对称字符串的方法
遍历整个字符串 取出所有首字母和尾字母相同的字符串加以判断
--------------------编程问答--------------------
求代码 --------------------编程问答--------------------
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();
}
哈哈,闲来无聊,帮楼主写了个 --------------------编程问答-------------------- 我也来一个
--------------------编程问答-------------------- public ArrayList GetValues(string s)
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;
}
{
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;
}
}
补充:.NET技术 , C#