gets,strcmp,strcat,strchr,strstr的应用


#include <stdio.h>
#include <string.h>


int main(void)
{
    char s1[80], s2[60];


    for (;;)
    {
        printf("请输入任意字符串:\n");
        gets(s1);  //获取键盘上输入的字符串,并将其依次放入s1[0]开头的数组中。


        printf("请再次输入任意字符串:\n");
        gets(s2);  //获取键盘上输入的字符串,并将其依次放入s2[0]开头的数组中。


        printf("Length: s1 = %d, s2 = %d\n", strlen(s1), strlen(s2));  //strlen(s1):获取s1的长度;strlen(s2):获取s1的长度


        if (!strcmp(s1, s2))
        /*strcmp(const1, const2):
         1.const1 == const2,返回0;
         2.const1 > const2,返回大于0值;
         3.const1 < const2,返回小于0值;
         */
            printf("The string are equal.\n");


        strcat(s1, s2);  //将s2链接到s1之后
        printf("%s\n", s1);


        strcpy(s1, "I got it\n");
        printf(s1);  //printf("%s\n", s1);与 printf(s1);功能相同

        if (strchr(s1, 'o'))
            printf("Found 'o' in s1\n");
        if (strstr(s1, "go"))
            printf("Found 'go' in s1\n");
    }


    return 0;
}
/*
 请输入任意字符串:
 i am lovely, is that?
 请再次输入任意字符串:
 yes, you are all right.
 Length: s1 = 21, s2 = 24
 i am lovely, is that? yes, you are all right.
 I got it
 Found 'o' in s1
 Found 'go' in s1
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值