字符串标准库函数的使用

字符串标准库函数的使用

/*
字符串长度计算函数(strlen)
字符串对比函数(strcmp)
字符串拼接函数(strcat)
字符串复制函数(strcpy)
*/

#include <stdio.h>
#include <string.h>
int main()
{    
    char buff[128]="qwer";
    char str[]="1234";
    //字符串长度计算函数(strlen)
    int len;
    len=strlen(str);
    printf("str的有效长度:%d\n",len);
    printf("str所占字节数:%d\n\n",sizeof(str));
    //字符串对比函数(strcmp)
    if(strcmp(buff,str)==0)
    {
        printf("str与buff相同。\n\n");
    }
    else
    {
        printf("str与buff不相同。\n\n");
    }
    //字符串拼接函数(strcat)
    printf("buff为:%s,str为:%s\n",buff,str);
    strcat(buff,str);
    printf("buff拼接str为:%s\n\n",buff);
    //字符串复制函数(strcpy)
    strcpy(buff,str);
    printf("str复制到buff为:%s\n",buff);
}

编译结果


/*编译结果:
str的有效长度:4
str所占字节数:5

str与buff不相同。

buff为:qwer,str为:1234
buff拼接str为:qwer1234

str复制到buff为:1234
*/

结论

//结论:
/*
(1)strlen返回值是字符串的有效长度,字符串的实际长度是有效长度+'\0'
(2)strcmp有返回值:0代表相同,1代表不同。
(3)strcat(1,2)返回链接后的字符串,是将2接在1的后面。
(4)strcpy(1,2)成功返回复制的字符串,失败返回NULL。将2复制到1,且先把1清空,再复制。
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值