当前位置: 首页 > ds >正文

C/C++常用字符串函数

一、字符串函数介绍:

字符串作为程序中常用的数据类型,学会对字符串进行处理是作为一名C/C++程序员的基本功,我们要学会使用相关函数,并且对重点函数要会自己手动实现(下文对重点函数有实现代码以及相关示例)

二、字符串函数详解

1.strlen:

作用:求字符串的大小

使用:

实现:

size_t MyStrlen1(const char* str)
{assert(str!=nullptr);int len = 0;while (str[len] != '\0')len++;return len;
}

示例:

int main()
{const char* str = "123456";printf("%d\n", strlen(str));printf("%d\n", MyStrlen1(str));return 0;
}

注意:strlen记录字符串函数大小,不包括'\0';

2.strcpy:

作用:将source字符串拷贝到destnation

使用:

实现:

char* MyStrcpy(char* destination, const char* source)
{assert(destination != nullptr);assert(source != nullptr);int pos = 0;while (source[pos] != '\0'){destination[pos] = source[pos];pos++;}destination[pos] = '\0';return destination;
}

示例:

int main()
{const char* str = "123456";char* str1 = (char*)malloc(strlen(str) + 1);printf("%s", strcpy(str1, str));printf("%s", MyStrcpy(str1, str));return 0;
}

3.strncpy:

作用:从source字符串拷贝指定长度到destnation字符串中

使用:

4.strcmp:

作用:字符串的比较

使用:

实现:

int MyStrcmp(const char* dst, const char* src)
{int pos1 = 0,pos2=0;while (dst[pos1]==src[pos2]&&dst[pos1]!='\0'){pos1++;pos2++;}return dst[pos1] - src[pos2];
}

示例:

int main()
{const char* str = "123456";const char* str1 = "123456";printf("%d\n", strcmp(str1, str));printf("%d", MyStrcmp(str1, str));return 0;
}

5.strncmp:

作用:指定长度的字符串比较

使用:

6.strcat:

作用:字符串的拼接

使用:

实现:

char* MyStrcat(char* destination, const char* source)
{assert(destination != nullptr);assert(source != nullptr);size_t pos1 = MyStrlen1(destination);size_t pos2 = 0;while (source[pos2] != '\0')destination[pos1++] = source[pos2++];destination[pos1] = '\0';return destination;
}

示例:

int main()
{char str[80];char str1[80];strcpy(str, "these ");MyStrcpy(str1, "these ");strcat(str, "strings ");MyStrcat(str1, "strings ");strcat(str, "are ");MyStrcat(str1, "are ");strcat(str, "concatenated.");MyStrcat(str1, "concatenated.");printf("%s\n", str);printf("%s\n", str1);return 0;
}

7.strncat:

作用:将source的指定长度字符串拼接到目标字符串末尾

使用:

8.strstr:

作用:查找子字符串在一个字符串的首次出现位置

使用:

实现:

char* MyStrstr(char* dst, const char* src)
{assert(dst);assert(src);int pos1 = 0;while (dst[pos1]!='\0'){int pos2 = 0;if (dst[pos1] == src[pos2]){int pos = pos1;while (dst[pos] == src[pos2]){pos++;pos2++;}if (src[pos2] == '\0')return dst + pos1;}pos1++;}return dst;
}

示例:

int main()
{char str[] = "This is a simple string";char str1[] = "This is a simple string";char* pch;char* pch1;pch = strstr(str, "simple");pch1 = MyStrstr(str1, "simple");if (pch != NULL)strncpy(pch, "sample", 6);if(pch1!=nullptr)strncpy(pch1, "sample", 6);printf("%s\n", pch);printf("%s", pch1);return 0;
}

9.strtok:

作用:将字符串按照指定的分割符切割成多个子串

使用:

示例:

int main()
{char str[] = "- This, a sample string.";char* pch;printf("Splitting string \"%s\" into tokens:\n", str);pch = strtok(str, " ,.-");while (pch != NULL){printf("%s\n", pch);pch = strtok(NULL, " ,.-");}return 0;
}

三、总结:

学会使用常见字符串的函数,处理好字符串等各种问题,对我们在项目工程等方面有非常重要的基础作用,例如解决算法问题、网络协议的制定、C++string类实现等等,当然本篇文章只讲述了常用的字符串函数,对于其他函数,有兴趣的朋友们可以看一下如下网址:<cstring> (string.h) - C++ Reference

还有一个简单的思维导图,方便大家复习巩固

结语:

以上就是我分享的字符串函数的全部内容了,希望对大家有些帮助,也希望与一样喜欢编程的朋友们共进步

谢谢观看

如果觉得还阔以的话,三连一下,以后会持续更新的,我会加油的

祝大家早安午安晚安

http://www.xdnf.cn/news/17035.html

相关文章:

  • 具身智能VLA困于“数据泥潭”,人类活动视频数据是否是“破局之钥”?
  • Noob靶机
  • 大模型 + 垂直场景:搜索 / 推荐 / 营销 / 客服领域开发有哪些新玩法?
  • 决策树算法:三大核心流程解析
  • 详解Python标准库之并发执行
  • 【王阳明代数讲义】基本名词解释
  • 机器学习消融实验:方法论演进、跨领域应用与前沿趋势
  • 海康皓视通 对接测试和比较
  • (吃饭)质数时间
  • AIDL当Parcelable序列化的数据类通信时报“Class not found when unmarshalling“找不到该类时的解决方案
  • JVM 01 运行区域
  • Python Pandas.from_dummies函数解析与实战教程
  • ubuntu双系统设置默认启动系统
  • Windows本地使用dify搭建知识库+ollama+deepseek
  • Java单元测试和设计模式
  • winscp 连openwrt 返回127错误码
  • InfluxDB 与 Node.js 框架:Express 集成方案(一)
  • 【网络原理】HTTP协议(一)
  • Chisel芯片开发入门系列 -- 17. CPU芯片开发和解释7(5级流水线指令原理)
  • 【Flutter3.8x】flutter从入门到实战基础教程(八):公共state的集中管理机制
  • WordPress AI写作插件开发实战:从GPT集成到企业级部署
  • 【Java】不允许直接操作数据表中的数据,开发前台界面来实现对多个数据表的增删改查
  • 【LeetCode刷题指南】--二叉树的前序遍历,二叉树的中序遍历
  • 力扣-105.从前序与中序遍历序列构造二叉树
  • Makefile 从入门到精通:自动化构建的艺术
  • 【人工智能agent】--服务器部署PaddleX 的 印章文本识别模型
  • 详解Python标准库之互联网数据处理
  • 电脑手机热点方式通信(下)
  • 基于OAuth2与JWT的微服务API安全实战经验分享
  • 【云计算】云主机的亲和性策略(四):云主机组