isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试
isPalindrome not showing output and isPalindrome function is not tested with the string input as the string argument
#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;
}
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存