【发布时间】:2021-12-18 12:17:19
【问题描述】:
【问题讨论】:
-
请查看您的 C++ 课程资料。正确的函数调用是
palindrome(a),使用前需要定义函数。 -
Please post text as text. 图像难以访问、难以搜索且难以处理。
标签: c++ string function boolean void
【问题讨论】:
palindrome(a),使用前需要定义函数。
标签: c++ string function boolean void
要调用布尔函数,只需在括号中输入函数名称和相关参数
这是一个例子:
bool isEven(int number){..}
可以调用
是偶数(3)
【讨论】:
您可以使用以下程序:
#include <iostream>
using namespace std;
//forward declare the function
bool palindrome (string a);
int main() {
string a;
cout<<"Masukkan kata : ";
cin>> a;
if (palindrome(a) == true)//call the function and check the return value.
{
cout<<"Kata tersebut termasuk palindrome ";
}
else
cout<<"Kata tersebut tidak termasuk palindrome";
}
bool palindrome (string a) {
int b;
b= a.length();
if (b == 0)
return 1;
else if (a[0] != a[b - 1])
return 0;
else
return palindrome (a.substr(1, b - 2));
}
以上程序的输出可见here。
【讨论】:
你应该在主函数之前定义函数。
#include <iostream>
using namespace std;
bool isPalindrome(std::string &s) {
// ...
return false;
}
int main() {
std::string s;
cin >> s;
if (isPalindrome(s)) {
std::cout << "..." << std::endl;
} else {
std::cout << "..." << std::endl;
}
return 0;
}
【讨论】: