我试图实现回文的解决方案,我认为我的逻辑是正确的,但是我的程序陷入了无限循环,并且我收到一条错误消息“Prep.exe 已停止工作”
int main()
{
string word, reverse;
cout << "Please enter a word to test for palindrome : ";
cin >> word;
cout << "Your word is: "<< word << endl;
int i = 0;
int size = word.length() - 1;
while (size >= 0)
{
reverse[i++] = word[size--];
//cout << reverse[i++];
}
cout << "The reversed word is: " << reverse << endl;
if (word == reverse)
cout << "It is palindrome" << endl;
else
cout << "It is not a palindrome" << endl;
}
我不确定我的 while 循环做错了什么。我继续递减它并且我有一个有效的退出条件,那么为什么它会陷入循环?