gpt4 book ai didi

c++ - isPalindrome 函数 C++ 问题

转载 作者:行者123 更新时间:2023-11-28 06:24:27 27 4
gpt4 key购买 nike

我正在编写一个程序,其中包含一个测试单词是否为回文的函数。编译器不会提示,程序也会运行,但不管输入什么字。有人能告诉我这是为什么吗?我什至从我正在使用的书中复制了 isPalindrome 函数。我不知道发生了什么语义问题。

#include <iostream>
#include <conio.h>
#include <string>
#include <cctype>

using namespace std;

bool isPalindrome (string str);

int main()
{
string word;

string str;

cout << "Enter a word to test if it is a palindrome. \n";
cin >> str;

for (int i=0; word [i] != '\0'; i++)
{ word[i] = tolower (word[i]); }

isPalindrome(word);

if (isPalindrome(word) == 1)
cout << "The word you entered is a palindrome." << endl;
if (isPalindrome(word) == 0)
cout << "The word you entered is not a palindrome." << endl;

_getch();
return 0;
}

bool isPalindrome(string str)
{
int length = str.length();

for (int i = 0; i < (length / 2); i++)
if (str[i] != str[length - 1 - i])
return false;

return true;
}

最佳答案

问题是您有两个字符串变量,“word”和“str”。您的输入存储在“str”中,但您使用“word”调用 isPalindrome

关于c++ - isPalindrome 函数 C++ 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28753282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com