<string.h>的学习

本文通过一个C语言示例程序,演示了多个字符串处理函数的实际应用,包括strcpy、strncpy、strcat、strchr等,并展示了如何使用这些函数进行字符串操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

感觉学习代码库最好的方法就是运行一下。

下面附上结果和示例代码 

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

int main(){
    const char * cs="Hello World";
    const char * ct="Hello";
    char * s=malloc(20);
    memset(s,0,20);
    char * t;
    int c='o';
    const size_t n=3;
    //strcpy(s,ct);
    printf("const char * cs=Hello World;\n\
    const char * ct=Hello;\n\
    int c=o;\n\
    const size_t n=3;\n\
    ");
    printf("=============================================\n");
    printf("\tthe result of strcpy(s,ct) is: %s\n",strcpy(s,ct));
    t=strncpy(s,ct,n);
    *(t+n)='\0';
    printf("\tthe result of char * strncpy(s,ct,n) is: %s\n",t);
    memcpy(s,ct,strlen(ct));//copy ct to s
    printf("\tthe result of char * strcat(s,ct) is: %s\n",strcat(s,ct));
    printf("\tthe result of char * strchr(cs,c) is: %s\n",strchr(cs,c));
    printf("\tthe result of char * strrchr(cs,c) is: %s\n",strrchr(cs,c));
    printf("\tthe result of size_t strspn(cs,ct) is: %d\n",strspn(cs,ct));
    printf("\tthe result of char * strpbrk(cs,ct) is: %s\n",strpbrk(cs,ct));
    printf("\tthe result of size_t strlen(cs) is: %d\n",strlen(cs));
}

 结果如下:

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值