C语言 字符串常用函数(计算长度,拷贝,拼接,比较)

strlen() 计算字符串长度(打印的是能看见的字符长度;sizeof()打印的是字符串物理长度,默认算上’\0’)

char testLen[] = "hello world";
        unsigned long count = strlen(testLen);
        printf("%lu\n", count);

strcpy() 字符串拷贝:把后面的字符串拷贝给前面的字符串 (并非把内容复制,而是拷贝指针)
原型:strcpy(char , const char )

    char testCpy1[] = " ";
        char testCpy2[] = "hello";
        strcpy(testCpy1, testCpy2);
        for (int i = 0; i < 6; i++) {
            printf("%c", testCpy1[i]);
        }

strcat() 字符串拼接
原型:strcat(char , const char )

    char testCat1[] = "hello";
        char testCat2[] = "world";
        strcat(testCat1, testCat2);
        printf("%s", testCat1);  //%s 输出字符串,按顺序扫描字符串,当遇到'\0'时,终止扫描

strcmp() 字符串比较
原型:strcmp(const char , const char )

    char testCmp1[] = "hello";
        char testCmp2[] = "hellwo";
        int result = strcmp(testCmp1, testCmp2);  //strcmp()函数需要一个返回值
        printf("%d", result);      //比较的是第一个不同的字母的ASCII码表的值
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值