C++ 回文程序总是给出 0(假)作为输出问题;我的代码哪里有问题?



问题是它总是输出0(假(。问题可能出在回文函数上,但我无法弄清楚确切的位置。如果有人帮忙,将不胜感激。

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
bool isPalindrome(string word)
{
bool result;
for (int i = 0; i <= word.length() - 1; i++)
{
if (word.at(i) == word.length() - 1)
{
result = true;
}
else
{
result = false;
}
return result;
}
}
int main()
{
string word1;
int count;
cout << "How many words do you want to check whether they are palindromes: " << flush;
cin >> count;
for (int i = 0; i < count; i++)
{
cout << "Please enter a word: " << flush;
cin >> word1;
cout << "The word you entered: " << isPalindrome(word1);
}
}

试试这个:

bool isPalindrome(string word)
{
bool result = true;
for (int i = 0; i < word.length() / 2; i++) //it is enough to iterate only the half of the word (since we take both from the front and from the back each time)
{
if (word[i] != word[word.length() - 1 - i]) //we compare left-most with right-most character (each time shifting index by 1 towards the center)
{
result = false;
break;
}  
}    
return result;
}

在此语句中

if (word.at(i) == word.length() - 1)

比较运算符的右侧表达式永远不会更改,并且类型为std::string::size_type而不是类型chaR。你是说

if (word.at(i) == word.at( word.length() - 1 - i ))

但是,使用成员函数是没有意义的。你可以使用下标运算符。例如

if ( word[i] == word[word.length() - 1 - i ] )

循环应该有 word.length((/2 次迭代。

同样在循环中,您将覆盖变量结果。因此,您始终返回变量的最后一个值。它可以等于 true,尽管字符串不是回文。

此外,参数应该是引用的类型。否则,将创建传递的参数的冗余副本。

函数可以通过以下方式定义

bool isPalindrome( const std::string &word )
{
std::string::size_type i = 0; 
std::string::size_type n = word.length();
while ( i < n / 2 && word[i] == word[n - i - 1] ) i++;
return i == n / 2;
}

另一种方法是以下

bool isPalindrome( const std::string &word )
{
return word == std::string( word.rbegin(), word.rend() );
}

尽管此方法需要创建原始字符串的反向副本。

最简单的方法是使用标准算法std::equal。这是一个演示程序

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
bool isPalindrome( const std::string &word )
{
return std::equal( std::begin( word ), 
std::next( std::begin( word ), word.size() / 2 ),
std::rbegin( word ) );
}
int main() 
{
std::cout << isPalindrome( "123454321" ) << 'n';
return 0;
}

我希望这个也能帮助你(也更正了警告(:

bool isPalindrome(string word)
{
bool result = false;
int lengthWord = (int)word.length();
for (int i = 0; i <= (lengthWord / 2); ++i)
{
if (word.at(i) == word.at(lengthWord - i -1))
{
result = true;
continue;
}
result = false;
}
return result;
}

两个可能的问题。

您似乎正在将字符与数字进行比较

if (word.at(i) == word.length() - 1)

这不应该是

if (word.at(i) == word.at(word.length() - i))

if 语句中有 3 个返回值,因此无论结果如何,在返回调用函数之前,它只会比较一个字符。

作为一种技术点,在循环中反复调用.length,总是返回相同的值,浪费时间并使代码更难以理解。

一旦发现不匹配,您需要立即返回。如果您正在寻找回文,则只需按相反的顺序将单词的前半部分与后半部分进行比较。类似的东西

bool isPalindrome(string word)
{
for (int i = 0, j= word.length() - 1; i<j; i++, j--)
// i starts at the beginning of the string, j at the end.
// Once the i >= j you have reached the middle and are done.
// They step in opposite directions
{
if (word[i] != word[j])
{
return false;
}              
}
return true;
}

函数isPalindrome中的循环只会执行一次,因为return语句在循环的第一次迭代中无条件执行。我确信这不是故意的。

要确定字符串是否为回文,必须多次执行循环。只有在计算了最后一个字符之后(在循环的最后一次迭代中(,才有使用 return 语句的时间,除非您事先确定字符串不是回文。

此外,在函数isPalindrome中,以下表达式是无意义的,因为您正在将字母的 ASCII 代码与字符串的长度进行比较:

word.at(i) == word.length() - 1

因此,我建议为该函数编写以下代码:

bool isPalindrome(string word)
{
for (int i = 0; i < word.length() / 2; i++)
{
if (word.at(i) != word.at( word.length() - i - 1) ) return false;
}
return true;
}

正如您的问题下的评论中所讨论的。您在代码中犯了一些错误。

您的函数应该或多或少如下所示:

bool isPalindrome(string word) { 
bool result = true; 
for (int i = 0; i <= word.length() - 1; i++)
{ 
if (word.at(i) != word.at(word.length() - 1 -i))     
{ 
return false; 
} 
} 
return result;
}

最新更新