两个关于字符串的笔试题:
1.求字符串是否为回文字符串(对称的字符串为回文字符串如:“ABA”,"ABBA")
public bool isPalindrome(string str)
{
if (str == null)
{
throw new Exception("字符串为null!!");
}
if (str == "")
{
return false;
}
for (int i = 0; i < (str.Length + 1) / 2; i++)
{
if (str[i] != str[str.Length - i - 1])
return false;
}
return true;
}
}
2.求一个字符串的最大回文的长度(可用第一题方法)
public static int getLenPalindrome(string str)
{
int max=0;
for(int i=0;i<str.Length-1;i++)
{
for (int j = i + 1; j < str.Length; j++)
{
string temp = str.Substring(i, j - i);
if (isPalindrome(temp))
max = temp.Length;
}
}
return max;
}