编写了一个使用示例6-6 (回文)中给出的函数isPalindrome的程序.在以下字符串上测试您的程序:
夫人,阿巴,22,67876,444244,幽会
修改示例6-6的函数isPalindrome,以便在确定字符串是否为回文时,忽略大小写,即大写字母和小写字母相同。
为了您的方便起见,下面包含了示例6-6中的isPalindrome函数。
bool isPalindrome(string str) { int length = str.length();for (int i= 0;i< length / 2;i++) { if (stri != strlength 1-i){返回false;}/ if }// for循环返回真;} // isPalindrome您的程序应该打印一条消息,指示字符串是否为回文:
夫人是回文
到目前为止我的计划是
#include <iostream>
#include <string.h>
using namespace std;
int main () {
bool isPalindrome (string str);
string str;
int length = str.length();
cout << "Enter a string: ";
getline (cin,str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length -1 -i]) {
cout << str << "Is not a Palindrome";
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) != islower(str[i])) {
cout << str << "Is a Palindrome";
} // for loop
return true;
}
}
我不知道我做错了什么,我发送了所有的东西,以确保它与单词向后匹配,然后当它是真的,它将返回为真。我对编程非常陌生,如果我的代码有点草率,我很抱歉。
发布于 2019-11-26 03:15:38
这是对代码的修改。你在里面声明这个函数并不太符合逻辑,所以我把它放在外面。
#include <iostream>
#include <string.h>
using namespace std;
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";
return false;
} else if (str[i] == str[length -1 -i] && toupper(str[i]) != islower(str[i])) {
cout << str << "Is a Palindrome";
} // for loop
return true;
}
return false;
}
int main () {
string str;
cout << "Enter a string: ";
getline (cin,str);
isPalindrome(str);
}
发布于 2021-09-06 11:23:10
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
发布于 2022-05-11 07:28:46
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 1000
bool isPalindrome(int x){
int c[MAX];
int i = 0;
int j;
int k = 0;
bool z;
if(x < 0){
return false;
}
while (x != 0){
int r = x % 10;
c[i] = r;
i++;
x = x / 10;
}
for (j = i - 1; j > -1; j--) {
printf("%d ", c[j]);
}
for(k = 0; k <= (i / 2); k++){
if(c[k] == c[i - k - 1]){
z = true;
}
else
{
z = false;
}
}
return z;
}
https://stackoverflow.com/questions/59043039
复制相似问题