目录

strlen函数

三种方法模拟实现:

1.计数器:

2.指针 - 指针

3.函数递归

strcpy函数

strncpy函数

strcat函数

strncat函数

strcmp函数

strncmp函数

strstr函数

strtok函数

strerror函数

字符转换函数

tolower函数

toupper函数

字符分类函数



strlen函数

  • 计算字符串长度;

  • size_t strlen ( const char * str );

  • 参数指向的字符串必须要以 '\0' 结束;

  • 字符串以'\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' );

  • 注意函数的返回值为size_t,是无符号的( 易错 );

三种方法模拟实现:

1.计数器:

#include<stdio.h>
#include<string.h>
#include<assert.h>
size_t my_strlen(const char* pc)
{assert(pc);size_t count = 0;while (*pc != '\0'){count++;pc++;}return count;
}
int main()
{char arr[] = { "abcdef" };size_t ret = my_strlen(arr);printf("%zd\n", ret);return 0;
}

2.指针 - 指针

#include<stdio.h>
#include<string.h>
#include<assert.h>size_t my_strlen(char* pc)
{char* ptr = pc;assert(pc && ptr);//让ptr指向字符串末尾while (*ptr != '\0'){ptr++;}return ptr - pc;
}
int main()
{char arr[] = { "abcdef" };size_t ret = my_strlen(arr);printf("%zd\n", ret);return 0;
}

3.函数递归

#include<stdio.h>
#include<string.h>
#include<assert.h>size_t my_strlen(const char* pc)
{assert(pc);if (*pc != '\0'){return 1 + my_strlen(pc + 1);}else{return 0;}
}
int main()
{char arr[] = { "abcdef" };size_t ret = my_strlen(arr);printf("%zd\n", ret);return 0;
}

strcpy函数

  • 字符串拷贝;

  • char* strcpy(char* destination, const char* source );

  • 源字符串必须以 '\0' 结束;

  • 会将源字符串中的 '\0' 拷贝到目标空间;

  • 目标空间必须足够大,以确保能存放源字符串;

  • 目标空间必须可变;

  • 如果原字符串的地址赋值给指针,其作为常量字符串时就不可被修改,只有改成数组存放它才能将其改掉;

使用实例:

#include<stdio.h>
#include<string.h>int main()
{char source[7] = {"abcdef\0"};char destination[10] = {"xxxxxxxxxx"};strcpy(destination, source);printf("%s\n", destination);return 0;
}

模拟实现

#include<stdio.h>
#include<string.h>
#include<assert.h>char* my_strcpy(char* destiniation, char* source)
{assert(source && destiniation);char* start = destiniation;while (*destiniation && *source){*destiniation = *source;destiniation++;source++;}//最后要将'\0'也放进去*destiniation = *source;return start;
}
int main()
{char source[] = { "hello" };char destination[10] = { "xxxxxxxxx"};my_strcpy(destination, source);printf("%s\n", destination);return 0;
}

优化模拟写法:

#include<stdio.h>
#include<string.h>
#include<assert.h>char* my_strcpy(char* destiniation, char* source)//完成了赋值、移位、判断
{assert(source && destiniation);char* start = destiniation;while (*destiniation++ = *source++){;}return start;
}
int main()
{char source[] = { "hello" };char destination[10] = { "xxxxxxxxx"};my_strcpy(destination, source);printf("%s\n", destination);return 0;
}

strncpy函数

  • 指定长度拷贝;
  • char * strncpy ( char * destination, const char * source, size_t num );

使用实例:

#include<stdio.h>
#include<string.h>int main()
{char arr1[] = { "hello" };char arr2[20] = { "world and you" };strncpy(arr2, arr1, 2);printf("%s\n", arr2);return 0;
}


strcat函数

  • 字符串追加;

  • char* strcat(char* destination, const char* source );

  • 源字符串必须以 '\0' 结束;

  • 目标空间必须有足够的大,能容纳下源字符串的内容;

  • 目标空间必须可修改;

  • 字符串自己给自己追加,如何?——> 不能给自己追加,\0被自己覆盖了,会死循环;

 使用实例:

#include<stdio.h>
#include<string.h>int main()
{char source[] = { " world" };char destination[20] = { "hello" };strcat(destination, source);printf("%s\n", destination);return 0;
}

模拟实现:

#include<stdio.h>
#include<string.h>
#include<assert.h>char* my_strcat(char* destination, const char* source)
{assert(destination && source);char* start = destination;//让destination指向末尾while (*destination){destination++;}//拷贝字符串while (*destination++ = *source++){;}return start;
}
int main()
{char source[] = { " world" };char destination[20] = { "hello" };my_strcat(destination, source);printf("%s\n", destination);return 0;
}

strncat函数

  • 指定长度追加;
  • char * strncat ( char * destination, const char * source, size_t num );
  • 会对原字符串追加结束后,在末尾添加 '\0' ;

使用实例:

#include<stdio.h>
#include<string.h>int main()
{char arr1[] = { "worldandyou" };char arr2[20] = { "hello \0xxxxxxx" };strncat(arr2, arr1, 5);printf("%s\n", arr2);return 0;
}


strcmp函数

  • int strcmp ( const char * str1, const char * str2 );

  • 字符串比较函数;

  • 第一个字符串大于第二个字符串,则返回大于0的数字;

  • 第一个字符串等于第二个字符串,则返回0;

  • 第一个字符串小于第二个字符串,则返回小于0的数字;

  • 比较的是每一对字节的ASCII值,而不是字符串长度,如果是汉字就是GBK;

使用实例:

#include<stdio.h>
#include<string.h>
#include<assert.h>int main()
{char arr1[] = { "hello" };char arr2[] = { "hallo" };printf("%d\n", strcmp(arr1, arr2));return 0;
}

一定注意不能这样写:这样只是在比较两个地址

模拟实现:

#include<stdio.h>
#include<string.h>
#include<assert.h>int my_strcmp(const char* pc1, const char* pc2)
{assert(pc1 && pc2);while (*pc1 == *pc2){if (*pc1 == '\0' || *pc2 == '\0'){return 0;}pc1++;pc2++;}return *pc1 - *pc2;
}
int main()
{char arr1[] = { "hallo" };char arr2[] = { "hello" };int ret = my_strcmp(arr1, arr2);printf("%d\n", ret);return 0;
}

strncmp函数

  • 指定长度比较;
  • int strncmp ( const char * str1, const char * str2, size_t num );

使用实例:

#include<stdio.h>
#include<string.h>int main()
{char arr1[] = { "hello world" };char arr2[20] = { "hello" };int ret = strncmp(arr1, arr2, 5);if (ret > 0){printf("arr1 > arr2\n");}else if(ret < 0){printf("arr1 < arr2\n");}else{printf("arr1 = arr2");}return 0;
}

strstr函数

  • char * strstr ( const char *str1, const char * str2);
  • 查找子串的函数
  • 找到返回子串第一个字符在原串里的地址
  • 找不到返回空指针NULL

使用实例:

#include<stdio.h>
#include<string.h>int main()
{char arr1[] = { "hello world" };char arr2[] = { "wor" };char* ret = strstr(arr1, arr2);if (ret == NULL){printf("子串不存在\n");}else{printf("%s\n", ret);}return 0;
}

模拟实现:

要考虑两种情况:

  • 子串在原字符串中直接出现:比如要在"abcdef" 中找到子串"def",直接遍历一次就可以找到;
  • 子串在原字符串中伪出现:比如要在"abcdeabdededef" 中找子串"def",前面出现的"de"会导致一次查找不完全;就需要借助标记指针;

#include<stdio.h>
#include<string.h>
#include<assert.h>char* my_strstr(const char* str1, const char* str2)
{assert(str1 && str2);const char* s1 = str1;const char* s2 = str2;const char* p = str1;while (*p){s1 = p;//设置str1的起始位置,将标志起点给到str1s2 = str2;//str2每次回到开头while (*s1 != 0 && *s2 != 0 && *s1 == *s2){s1++;s2++;}//找到了,因为str2已经到了末尾if (*s2 == 0){return (char*)p;}p++;//每查找一轮,如果发现不完全相同,p就向后走一步,更改标志起点}return NULL;
}
int main()
{char arr1[] = { "hello wowowowowoworld" };char arr2[] = { "wor" };char* ret = my_strstr(arr1, arr2);if (ret == NULL){printf("子串不存在\n");}else{printf("%s\n", ret);}return 0;
}

strtok函数

  • 切割字符串;
  • char * strtok ( char * str, const char * sep );
  • sep参数是个字符串,定义了用作分隔符的字符集合;
  • str参数是要被切割的字符串;

使用实例:

#include<stdio.h>
#include<string.h>int main()
{char str[] = "www.Main@csdn.com";char sep[] = { ".@" };//先把原字符串拷贝一份,安全起见不要在原字符串上切割char cpy[20] = { 0 };strcpy(cpy, str);char* ret = strtok(cpy, sep);printf("%s\n", ret);ret = strtok(NULL, sep);printf("%s\n", ret);ret = strtok(NULL, sep);printf("%s\n", ret);ret = strtok(NULL, sep);printf("%s\n", ret);return 0;
}

优化:如果字符串非常长,我们不可能一直写printf,就可以借助循环来打印;

#include<stdio.h>
#include<string.h>int main()
{char str[] = "www.Main@csdn.com";char sep[] = { ".@" };//先把原字符串拷贝一份,安全起见不要在原字符串上切割char cpy[20] = { 0 };strcpy(cpy, str);char* ret = NULL;for (ret = strtok(str, sep); ret != NULL; ret = strtok(NULL, sep)){printf("%s\n", ret);}return 0;
}

strerror函数

  • 返回错误码对应的错误信息;
  • char * strerror ( int errnum );

使用实例:

#include<stdio.h>
#include<string.h>int main()
{printf("%s\n", strerror(0));printf("%s\n", strerror(1));printf("%s\n", strerror(2));printf("%s\n", strerror(3));printf("%s\n", strerror(4));printf("%s\n", strerror(5));printf("%s\n", strerror(6));printf("%s\n", strerror(7));return 0;
}

 实例2:

#include<stdio.h>
#include<string.h>
#include<errno.h>int main()
{FILE* pf = fopen("test.txt", "r");if (pf == NULL){printf("%s\n", strerror(errno));}else{//...}return 0;
}


字符转换函数

tolower函数

  • 大写转小写;
  • int tolower (int c);
#include<stdio.h>
#include<string.h>int main()
{char ch = 'A';printf("%c\n", tolower(ch));return 0;
}

toupper函数

  • 小写转大写;
  • int toupper(int c);
#include<stdio.h>
#include<string.h>int main()
{char ch = 'a';printf("%c\n", toupper(ch));return 0;
}

字符分类函数

isspace 空白字符:空格、换页、换行、回车、制表符等
isdigit 十进制数字0 - 9
  isxdight   十六进制数字,包括所有十进制数字,大小写字母
islower 小写字母a - z
isupper 大写字母A - Z
isalpha 字母a - z 或者 A- Z
isalnum 字母或者数字,字母a - z 或者 A- Z 或者 0 - 9
ispunct 标点符号
isgraph 任何图形符号
isprint 任何可打印字符

以isspace函数举例:

  • 判断是否为:空格' ' 、换行'\n'、回车'\r'、制表符等;
  • int isspace( int c );
  • 如果c是空白字符(0x09–0x0D或0x20),则isspace返回非零值。如果c是一个与标准空白字符相对应的宽字符,或者是实现定义的一组宽字符之一(iswalnum为false),则iswspace返回非零值。如果c不满足测试条件,这些例程中的每一个都返回0。
#include<stdio.h>
#include<ctype.h>int main()
{char ch = 0;scanf("%c", &ch);printf("%d\n", isspace(ch));return 0;
}

除了本篇关于C语言常用字符串函数外,更有java常用String类方法的归类:

Java——String类常见方法_@Main.的博客-CSDN博客

C语言字符、字符串函数(超详细版)相关推荐

  1. C语言文件操作(超详细版)

    目录 什么是文件 ✨文件分类 程序文件 数据文件 文件的使用 ✨文件指针 文件指针的使用 ✨文件的打开和关闭 文件的使用方式 ✨文件的顺序读写 1.写入一个字符 2.读取一个字符 3.连续每次读取一个 ...

  2. C语言学生管理系统图文超详细版(拷贝即用)

    花哥哥的瞎扯: 大一的课设学生管理系统,因为学也学不精,望各位大神轻点骂,多指点一下吖.注释写得相对的仔细,认真看玩就没有不会的!哈哈哈加油! 实现功能: 建立学生结点之后再弄一个链表,用尾插法的方式 ...

  3. Redis 超详细版教程笔记

    视频教程:[狂神说Java]Redis最新超详细版教程通俗易懂 视频地址:https://www.bilibili.com/video/BV1S54y1R7SB 目录索引 nosql 阿里巴巴架构演进 ...

  4. Tars环境搭建(超详细版)

    Tars环境搭建(超详细版) 简介 Tars是基于名字服务使用Tars协议的高性能RPC开发框架,同时配套一体化的服务治理平台,帮助个人或者企业快速的以微服务的方式构建自己稳定可靠的分布式应用. Ta ...

  5. c语言字符函数isalpha,总结C语言字符检测函数:isalnum、isalpha...

    前言:最近一直在刷leetcode的题,用到isalnum函数,用man手册查找了一下,总共有13个相关函数如下: #include int isalnum(int c); int isalpha(i ...

  6. new是不是c语言运算符优先级表,C语言运算符优先级列表(超详细)

    <C语言运算符优先级列表(超详细)>由会员分享,可在线阅读,更多相关<C语言运算符优先级列表(超详细)(7页珍藏版)>请在人人文库网上搜索. 1.本篇文章是对C语言中运算符的优 ...

  7. C语言常用字符串函数strlen、strcpy、strcat、strcmp、strchr

    C语言常用字符串函数,求串长strlen(char *s).串复制strcpy(char *s1,char *s2).串连接strcat(char *s1,char *s2).串比较strcmp(ch ...

  8. 【C语言】字符串函数详解

    hello~~,我是~小鹿 ,这是我的第一篇博客,没有循序渐进从基础开始写,只是最近在学习这里就写了,比较随心吧.希望这一篇博客能够给你带来帮助,之后也会继续写的,只是可能没有循序渐进,会比较杂七杂八 ...

  9. Docker超详细版教程通俗易懂 -之- 入门篇

    前言 学习Docker,你可以熟练的操作命令,能够把你的项目构建成Docker镜像! 是后端开发人员必备的技能!下面是自己的学习笔记,希望能帮助到需要的你! 特别感谢哔哩哔哩狂神:[狂神说Java]D ...

最新文章

  1. echo打印彩色的用法
  2. Android ProgressBar 不能在Button上面显示
  3. 移动端点击事件延迟300毫秒
  4. nginx利用proxy_cache来缓存文件
  5. BZOJ 1500 维修数列
  6. nyoj 71 独木舟上的旅行 贪心
  7. linux应用参数 冒号,Lua-面向对象中函数使用时冒号(:)和点(.)的区别
  8. Async Program 基本知识 (Process、Thread、Context Switch)
  9. phalapi-进阶篇1(Api,Domain,和Model)
  10. 怎么用matlab算矩阵行列式的值,新手如何利用matlab软件进行简单的矩阵运算 值得一看...
  11. python中easygui有几种_一、Python 模块EasyGui详细介绍
  12. 基于JAVA+SpringBoot+Mybatis+MYSQL的校园兼职招聘系统
  13. sas中一些小的选项的含义
  14. asp.net 母版页使用方法
  15. 美国西北大学 计算机工程专业排名,权威首发!2018年USNews美国大学研究生计算机工程专业排名榜单...
  16. HDOJ-1272 小希的迷宫
  17. swfobject参数详解
  18. 【渝粤教育】广东开放大学 建筑工程计量与计价 形成性考核 (47)
  19. python aic准则_pythonAIC准则下线性回归实现及模型检验案例分析
  20. Microsoft Edge浏览器黑色背景修改

热门文章

  1. 更相减损术——Java实现
  2. 【199天】黑马程序员27天视频学习笔记【Day15-16复习脑图】
  3. 2014~2015,英语硕果盘点
  4. pyqt5 QListWidget QListWidgetItem例子
  5. 容器数据卷,Docker安装Mysql5.7以及MySQL主从搭建过程
  6. springdata-elasticsearch官方文档
  7. 天邑TY1608-高安版-905L3B-EMMC-当贝纯净桌面-免拆卡刷固件包
  8. 告别从删库到跑路,linux回收站实现
  9. 解决latex 出现File ended while scanning use of \BR@@bibitem.
  10. android xutils json请求,Android Xutils3网络请求的封装详解及实例代码