这里的问题是它不能是来自用户输入的字符串。共有7个字符串,其中6个是数字,1个是“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 << "
这样的东西不是回文。“;
答案 0 :(得分:3)
您使用str = "one", "two", "three";
将str
设置为"three"
... ,
运算符即可。此外,str
可以包含一个字符串,尝试更多只是不起作用。你指定给(未定义的)变量IsNumPalindrome
的名称IsNum
是指向函数的指针,如果你再问if(IsNum)
它将不会是空指针,所以总是如此真。
我可以继续。似乎有一条线路没有严重错误或严重误解C ++。
答案 1 :(得分:0)
string str;
str = "10", "32", "222", "abba", "444244", "67867876", "123454321";
更改为
std:vector< std::string > vecOfStrings = { "10", "32", "222", "abba", "444244", "67867876", "123454321" };
然后循环遍历向量并将每个字符串传递给函数
for (unsigned int i = 0; i < vecOfStrings.size(); i++) {
if ( isNumPalindrome( vecOfStrings[i] ) ) {
// do something
}
}