这是问题:
“编写一个程序,从文本中提取所有回文单词,例如ABBA、lamal、exe。”
以下是我的代码:
public static List<string> FindPalindromes()
{
string text = String.Empty;
Console.Write("Enter the text:\n\t");
text = Console.ReadLine();
List<string> answer = new List<string>();
string[] words = text.Split('.', ',', ' ', ':', '/', '\\', '"', ';');
foreach(string word in words.Where(
(string x) =>
{
if(String.Equals(x, x.Reverse()))
return true;
else
return false;
}
))
answer.Add(word);
return answer;
}
现在我认为如果我将where方法中的逻辑分离成一个单独的方法,返回一个布尔值并检查单个单词是否是回文会更加整洁。但我想尝试使用lambda表达式。
无论如何,这段代码没有返回任何东西。我怀疑问题出在if条件语句中。
x => String.Equals(x, x.Reverse())
。 - clcto