树莓派编程:从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;
超级会员免费看
订阅专栏 解锁全文
64

被折叠的 条评论
为什么被折叠?



