namespace Palindrome
{
class Program
{
public static bool IsPalindrome(string s)
{
int min = 0;
int max = s.Length - 1;
while (true)
{
if (min > max) // True if we've compared up to, and then gone passed the middle of the string.
return true;
if (char.ToLower(s[min++]) != char.ToLower(s[max]))
return false;
}
}
static void Main(string[] args)
{
string [] words = {
"civic",
"deified",
// ...
"stats",
"tenet",
};
foreach (string value in words)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey(true); }
}
}
단어 배열에있는 단어가 회상인지 확인합니다 (단어는 거꾸로 동일한 전진 철자).회문 문 만들기
메인의 foreach
루프는 배열의 각 단어를 IsPalindrome()
함수로 전달합니다. 단어를 테스트하고 그에 따라 True 또는 False를 반환합니다.
현재 배열의 각 단어가 Palindrome이므로 프로그램을 실행하면 모든 현재 단어가 출력되고 True가 뒤따라야합니다. 그러나, 그것은 나에게 False를 준다. 왜 그런가요?
장소 중단 점을 이동하여 코드를 통해 F10 단계를 사용하고 잘못 어디를 참조하십시오. – CodeCaster
문제는 쉽게 디버깅으로 해결할 수 있습니다. 더 좋은 방법으로, 당신은 [당신의 문자열을 뒤집을 수 있습니다] (http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string) 원래의 것을 비교하십시오. –
왜 ToLower()를 사용하고 있습니까? – Jodrell