week3-day9作业

Description

读取一个字符串,字符串可能含有空格,将字符串逆转,原来的字符串与逆转后字符串相同,输出0,原字符串小于逆转后字符串输出-1,大于逆转后字符串输出1。例如输入 hello,逆转后的字符串为 olleh,因为hello 小于 olleh,所以输出-1

注意最后的判断一定要这么写,因为strcmp标准C中并不是返回-1和1,而是负值和正值

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);

}

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>

int main()
{
    char c[100], d[100] = {0};

    //fgets(c, sizeof(c), stdin);
    //与gets相比,fgets在'\0'之前多了一个'\n',可以配合以下语句
    //int len = strlen(c);
    //c[len - 1] = '/0';

    //输入一串可含空格的字符串:
    gets(c);

    //puts(c)用于调试,输出一串可含空格的字符串:
    //puts(c);

    int i, j=0;
    //printf("%d", strlen(c));//字符串长度(不包含结束符)
    for (i = strlen(c) - 1; i >=0; i--)
    {
        //printf("%c\n", c[i]);
        d[j] = c[i];
        j++;
    }
    //puts(d);

    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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值