news 2026/5/10 12:22:13

寒假学习(7)(C语言7+模数电7)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
寒假学习(7)(C语言7+模数电7)

#include <stdio.h> #include <string.h> int main() { //strspn函数怎么用 //函数说明: //strspn函数返回字符串str1中第一个不在字符串str2中出现的字符下标。 char *str = "123456789"; char *str2 = "23"; printf("%d\n", strspn(str, str2)); return 0; } 一键获取完整项目代码 cpp #include <stdio.h> #include <string.h> int main() { #if 0 //strstr函数怎么用 //函数说明: //strstr函数返回字符串str2在字符串str1中第一次出现的位置。 char *str = "123456789"; char *str2 = "23"; printf("%s\n", strstr(str, str2)); #endif //使用strstr函数统计字串的个数 char *str = "123223323423"; char *str2 = "23"; int count = 0; char *p = str; while(p!=NULL) { p = strstr(p, str2); if(p!=NULL) { count++; p += strlen(str2); } } // while ((p = strstr(p, str2)) != NULL) // { // count++; // p += strlen(str2); // } printf("%d\n", count); return 0; }
#include <stdio.h> #include <string.h> int main() { //strtok函数怎么用 //函数说明: //strtok函数将字符串str1分割成由字符串str2分割的多个部分,并返回分割后的第一个部分。 char str[] = "i am from china"; // 使用数组而非字符串字面量,允许修改 char *delim = " "; // 分隔符字符串 char *result = strtok(str, delim); while (result != NULL) { // 检查返回值 printf("%s\n", result); result = strtok(NULL, delim); // 继续分割 } return 0; }
#include <stdio.h> #include <string.h> //实现strtok_r函数 char* strtok_r(char* str, const char* delim, char** saveptr) { if (str == NULL) { str = *saveptr; } if (str == NULL) { return NULL; } while(delim != NULL) { if (*delim == *str) { *str = '\0'; *saveptr = str + 1; return str; } delim++; } } int main() { char str[] = "i am from china"; // 使用数组而非字符串字面量,允许修改 char *delim = " "; // 分隔符字符串 char *saveptr = NULL; char *result = strtok_r(str, delim, &saveptr); while (result != NULL) { // 检查返回值 printf("%s\n", result); result = strtok_r(NULL, delim ,&saveptr); // 继续分割 } return 0; }
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { //函数指针的作用 //函数指针是指向函数的指针,可以通过函数指针调用函数。 int (*p)(int, int) = add; printf("%d\n", p(1, 2)); //1.隐藏接口 //2. 间接体现多态 return 0; }


#include <stdio.h> int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a * b; } int main() { //函数指针数组怎么用 //函数指针数组是指一个数组,数组中的每个元素都是一个函数指针。 int (*p[3])(int, int) = {add, sub, mul}; printf("%d\n", p[0](1, 2)); printf("%d\n", p[1](1, 2)); printf("%d\n", p[2](1, 2)); return 0; }
#include <stdio.h> //什么是可变参数 /** * 实现原理 * 变量压栈是连续的 * C语言提供了三个宏定义 * va_start(ap, n) 初始化ap * va_arg(ap, type) 获取参数 * va_end(ap) 结束 */ #include <stdarg.h> int add(int n, ...) //以一个参数n表示可变参数的个数 { int sum = 0; va_list ap; va_start(ap, n); for (int i = 0; i < n; i++) { sum += va_arg(ap, int); } va_end(ap); return sum; } int main() { //可变参数怎么用 //可变参数是指一个函数可以接收任意个数的参数。 printf("%d\n", add(3, 1, 2, 3)); return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/10 5:08:45

打造专属自托管启动页:一站式服务集成仪表板搭建指南

打造专属自托管启动页&#xff1a;一站式服务集成仪表板搭建指南 【免费下载链接】homepage 一个高度可定制的主页&#xff08;或起始页/应用程序仪表板&#xff09;&#xff0c;集成了Docker和服务API。 项目地址: https://gitcode.com/GitHub_Trending/ho/homepage 在…

作者头像 李华
网站建设 2026/5/9 19:46:54

30分钟掌握WebSocket实战:构建gin-vue-admin实时通信系统

30分钟掌握WebSocket实战&#xff1a;构建gin-vue-admin实时通信系统 【免费下载链接】gin-vue-admin 项目地址: https://gitcode.com/gh_mirrors/gin/gin-vue-admin 在现代Web应用开发中&#xff0c;实时通信已成为提升用户体验的关键技术。传统的HTTP轮询方案不仅延迟…

作者头像 李华
网站建设 2026/5/9 21:23:34

形式化验证工具如何重塑软件开发?我的Lean 4探索日志

形式化验证工具如何重塑软件开发&#xff1f;我的Lean 4探索日志 【免费下载链接】lean4 Lean 4 programming language and theorem prover 项目地址: https://gitcode.com/GitHub_Trending/le/lean4 作为一名金融科技公司的软件工程师&#xff0c;我永远忘不了那次因为…

作者头像 李华
网站建设 2026/5/6 10:59:58

Python类型检查新范式:基于BasedPyright的智能开发解决方案

Python类型检查新范式&#xff1a;基于BasedPyright的智能开发解决方案 【免费下载链接】basedpyright pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server 项目地址: https://gitcode.co…

作者头像 李华