回文串是从左到右或者从右到左读起来都一样的字符串,试编程判别一个字符串是否为回文串。
标签:style blog http io ar color os sp for
http://oj.hi-hi.cn/JudgeOnline/problem.php?cid=1088&pid=12
回文串是从左到右或者从右到左读起来都一样的字符串,试编程判别一个字符串是否为回文串。
输入一个字符串。串长度<255.
判别输入的字符串是否为回文串,是输出"Y",否则输出"N"。
学生问 ,下面的代码为啥答案错误 ,自己试都可以来着
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <ctype.h> 4 #include <string.h> 5 int main() 6 { 7 char c[255],i,a,n; 8 while(gets(c)!=NULL) 9 { 10 n=strlen(c); 11 char b[n+1]; 12 a=n-1; 13 for(i=0; i<n; i++) 14 { 15 b[i]=c[a]; 16 a--; 17 } 18 b[n]=‘\0‘; 19 20 if(strcmp(b,c)==0) 21 printf("Y\n"); 22 else 23 printf("N\n"); 24 } 25 return 0; 26 }
答案:
把a,i, n改成整型即可。 char的符号是signed还是unsigned是实现定义的,与编译器相关,字符长度可以到127以上, 如果是signed负数,就会出现负下标,而不是预期的正下标,负下标是往前走的,显然和预期不符。
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/4bytes/p/4136313.html