读取一个字符串,字符串可能含有空格,将字符串逆转,原字符串与逆转字符串进行比较@C语言

读取一个字符串,字符串可能含有空格,将字符串逆转

原来的字符串与逆转后字符串比较相同,输出0,

原字符串小于逆转后字符串输出-1,

大于逆转后字符串输出1。

例如输入 hello,逆转后的字符串为 olleh,因为hello 小于 olleh,所以输出-1

Sample Input 1 

hello

Sample Output 1

-1
#include 
#include 
int main(){
    char str[20];
    char reStr[20];
    int j=0;
    gets(str);
    int len=strlen(str);

    for (int i = len-1; i >=0 ; i--) {//将str数组逆置
        reStr[j++]=str[i];
    }

    int cmpResult=strcmp(str,reStr);
    if(cmpResult<0){
        printf("%d\n",-1);
    }
    else if(cmpResult>0){
        printf("%d\n", 1);
    }
    else{
        printf("%d\n", 0);
    }
    }

你可能感兴趣的:(代码练习,C语言,算法,学习,c语言,开发语言)