【发布时间】:2013-04-29 14:34:24
【问题描述】:
这里的问题是它不能是用户输入的字符串。有7个字符串,其中6个是数字,一个是单词“abba”。到目前为止,我已经编写了很多代码,但我无法找到一种方法来测试我必须用于程序的 7 个字符串。
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
bool isNumPalindrome(string str);
int main ()
{
string str;
str = "10", "32", "222", "abba", "444244", "67867876", "123454321";
int userExit;
bool isNum = isNumPalindrome;
if (isNumPalindrome)
{
cout << str << " is a palindrome";
cout << endl;
}
else
{
cout << str << " is not a palindrome";
cout << endl;
}
cout << "Press any key to exit: ";
cin >> userExit;
cout << endl;
return 0;
}
bool isNumPalindrome(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;
}
如您所见,我还没有完全弄清楚如何在 main 中执行一个函数来获取返回并输出一条语句。我需要了解如何测试多个字符串,以及那么如何使用return语句打印cout << str << "之类的东西不是回文。”;
【问题讨论】:
-
另外我知道我已经包含了不需要的标题,并且我在声明 isNum 为 isNumPalindrome 的地方停止了,所以它已经不正确了。
-
您需要阅读有关如何在 C++ 中调用和使用函数的知识。这有很多问题。
-
我知道它的边缘非常粗糙。如果我被允许使用用户输入,我不会有任何问题。但我开始了,这让我失望了。
-
非常粗糙?这在很多方面都是一团糟……您需要查看一些 c++ 教程。你似乎对数组、函数等一无所知。
标签: function return-value palindrome