20、树莓派编程:从C到C++及面向对象编程基础

树莓派编程:从C到C++及面向对象编程基础

1. C语言字符串操作与LED控制

在C语言编程中,字符串操作是基础且重要的部分。以下是一个字符串操作的示例代码:

// find and replace the w with a W
char *p = strchr(c,'w');  // returns pointer to first 'w' char
*p = 'W';
printf("The string c is now: %s\n", c);
if (strcmp("cat", "dog")<=0){      // ==0 would be equal
    printf("cat comes before dog (lexiographically)\n");
}
//insert "to the" into middle of "Hello World!" string - very messy!
char *d = " to the";
char *cd = malloc(strlen(c) + strlen(d));
memcpy(cd, c, 5);
memcpy(cd+5, d, strlen(d));
memcpy(cd+5+strlen(d), c+5, 6);
printf("The cd string is: %s\n", cd);
//tokenize cd string using spaces
p = strtok(cd," ");
while(p!=NULL){
    printf("Token:%s\n", p);
    p = strtok(NULL, " ");
}
return 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值