王道oj训练总结
作业5-2
问题描述
Description
读取一个字符串,字符串可能含有空格,将字符串逆转,原来的字符串与逆转后字符串相同,输出0,原字符串小于逆转后字符串输出-1,大于逆转后字符串输出1。例如输入 hello,逆转后的字符串为 olleh,因为hello 小于 olleh,所以输出-1
注意最后的判断一定要这么写,因为strcmp标准C中并不是返回-1和1,而是负值和正值
Input
输入一个字符串,例如 hello,当然输入的字符串也可能是 how are you,含有空格的字符串
Output
输出是一个整型数,如果输入的字符串是hello,那么输出的整型数为-1
Sample Input 1
hello
Sample Output 1
-1
Sample Input 2
cba
Sample Output 2
1
Sample Input 3
aba
Sample Output 3
0
代码示例
#include <stdio.h>
#include <string.h>
#define N 1000
int main(){
char c[N],d[N];
gets(c);
int length = strlen(c);
int j;
for (int i = length-1; i >=0 ; i--) {
d[j]=c[i];
j++;
}
int result = strcmp(c, d);
if (result < 0)
{
printf("%d\n",-1);
}
else if (result > 0)
{
printf("%d\n", 1);
}
else {
printf("%d\n", 0);
}
return 0;
}
总结
注意数组的下标,在for循环遍历数组的时候要注意length-1这个条件.
注意strcmp函数语法为“int strcmp(char *str1,char *str2)”,其作用是比较字符串str1和str2是否相同,如果相同则返回0,如果不同,前者大于后者则返回1,否则返回-1。