题目链接: http://acm.jlu.edu.cn/joj/showproblem.php?pid=1008
题目大意: 判断一个带有标点,字母,空格的字符串是不是回文,忽略空格和标点,只比较字符
特殊情况: 1. 没有字符时,输出不是
2. 要是全是标点,不是回文
我的错误:
判断是不是大写字母,注意判断的时候要考虑逻辑运算的执行方式
要是 ('A' > a[i] || 'z' < a[i]) && ('a' > a[i] || 'z' < a[i]);
上面判断方式是这个字母即不是大写字母,也不是小写字母。
但是如果是小写,第一个就会false,判断小写的就更不会判断,而且逻辑混乱。还是经验不够啊!
AC程序:
#include "iostream"
#include <string>
#include <ctype.h>
#include <cstdio>
using namespace std;
char a[1024];
//判断是不是大写字母
int isAlpha(int i){
return ('A' <= a[i] && 'z' >= a[i]) || ('a' <= a[i] && 'z' >= a[i]);
}
int main(){
//freopen("in.txt", "r", stdin);
int iCase;
cin>>iCase;
getchar();
while(iCase--){
gets(a);
int i = 0, j = strlen(a) - 1;
//处理空字符的情况,直接回车
if(strcmp(a, "\0") == 0){
cout<<a<<" is not a palindrome."<<endl;
continue;
}
while(i <= j){
while(!isAlpha(i))
i++;
while(!isAlpha(j))
j--;
if(i > j)
break;
if(tolower(a[i]) != tolower(a[j])){ //转换成小写函数,在ctype.h中定义
cout<<a<<" is not a palindrome."<<endl;
break;
}
i++;
j--;
}
//没有字符的情况
if(i > strlen(a))
cout<<a<<" is not a palindrome."<<endl;
else if(i > j)
cout<<a<<" is a palindrome."<<endl;
}
return 0;
}