isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试

isPalindrome not showing output and isPalindrome function is not tested with the string input as the string argument

本文关键字:isPalindrome 字符串 参数 测试 输入 输出 函数 未使用 显示      更新时间:2023-10-16
#include <iostream>
#include <string.h>
using namespace std;
void convert(string& str) {
for (size_t i =0; i <str.length(); i++) {
str[i] = tolower(str[i]);
}
}
bool isPalindrome(string str) {
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << " is not a palindrome" << endl;
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) == toupper(str [length -1 -i])) {
cout << str << " is a palindrome" << endl;
} // for loop
return true;
}
return false;
}
int main () {
string str;
getline(cin, str);
convert(str);
isPalindrome(str);
return 0;
}

出于某种原因,我的编码作业的家庭作业评分器中的输出框显示为空白,但是当我运行我的程序时,它会显示它是否是回文。此外,自动评分器还会在我的代码中搜索特定的模式,例如 .+isPalindrome(\"Madam\"(.+,但自动评分器说我只得到了 10 分,因为我的代码有一个特定的模式 \s\isPalindrome(字符串 str(。我真的很困惑,不知道为什么我的输出显示为空白,而自动评分器正在寻找特定的模式,但我不知道是什么模式???

您只是误解了输出终端。一切正常,只需要求用户输入一个值即可。

#include <iostream>
#include <string.h>
using namespace std;
void convert(string& str) {
for (size_t i =0; i <str.length(); i++) {
str[i] = tolower(str[i]);
}
}
bool isPalindrome(string str) {
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << " is not a palindrome" << endl;
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) == toupper(str [length -1 -i])) {
cout << str << " is a palindrome" << endl;
} // for loop
return true;
}
return false;
}
int main () {
string str;
cout<<"Enter a string to check is it palindrome or not."<<endl;
getline(cin, str);
convert(str);
isPalindrome(str);
return 0;
}

评分员想要查看代码模式,因此我们必须逐个检查每个字符串。

#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string str);
int main()
{
if (isPalindrome("Madam"))
cout << "Madam" << " is a palindrome." << endl;
else
cout << "Madam" << " is not a palindrome." << endl;

if (isPalindrome("abBa"))
cout << "abBa" << " is a palidrome." << endl;
else
cout << "abBa" << " is not a palindrome." << endl;
if (isPalindrome("22"))
cout << "22" << " is a palindrome." << endl;
else
cout << "22" << " is not a palindrome," << endl;
if (isPalindrome("67876"))
cout << "67876" << " is a palindrome." << endl;
else
cout << "67876" << " is not a palindrome." << endl;
if (isPalindrome("444244"))
cout << "444244" << " is not a palindrome." << endl;
else
cout << "444244" << " is not a palindrome." << endl;
if (isPalindrome("trYmeuemyRT"))
cout << "trYmeuemyRT" << " is a palindrome." << endl;
else
cout << "trYmeuemyRT" << " is not a palindrome." << endl;
string aStr[] = {"madam",
"abba", 
"22",
"67876",
"444244", 
"trymeuemyrt"};
for (int i=0; i<static_cast<int>(sizeof(aStr)/sizeof(aStr[0])); i++) {

if(isPalindrome(aStr[i]))
cout << aStr[i] << " is a palindrome." << endl;
else
cout << aStr[i] << " is not a palindrome." << endl;
}
return 0;
}
bool isPalindrome(string str)
{
int length = str.length();
for (int i = 0; i < length / 2; i++){
if (tolower(str[i]) != tolower(str[length - 1 - i]))
return false;
}
return true;
}