一、前言
这一期的博客,将会着重讲解常见的字符或字符串的相关库函数。C语言中有一组库函数专门用来处理字符类型的数据的,后文则会介绍字符分类函数以及字符转换函数。字符串相关的函数是本篇博客的重点,这也是为什么标题是字符串函数详解。我们不但会学习这些库函数的使用,而且对一些重要的库函数进行模拟实现,以学习他的算法思想也是重中之重,这对我们日后的C语言的学习的提升有着很大的帮助。字符函数的头文件:<ctype.h>字符串函数的头文件:<string.h>二、字符函数
1.字符分类函数:
用来判断一个字符是否符合规定的某一类型要求,若符合规定某一类型的要求,则会返回非0的数字,也就是真,若不符合则返回0,也就是假。ES:isspace(),用来判断输入的字符的类型是否是空白字符,若是空白字符则会返回非0值,否则返回0;

2.字符转换函数:
int tolower(int c),大写字母转小写字母;int toupper(int c),小写字母转大写字符。
三、字符串函数
1.strlen
函数声明:size_t strlen(const char* string);函数功能:获取一个字符串的长度,其长度指的是字符串结束标志( '\0' )之前的字符个数包括空白字符。模拟实现:(更多的是模拟实现算法,请点击下面链接)
代码案例:

2.strcpy
函数声明:char* strcpy(char* dest,const char* sour);函数功能:将sour中的字符串内容拷贝到dest中,包括('/0' );遇到'/0' 时拷贝结束。模拟实现:

3.strcat
函数声明:char* strcat(char* dest,const char* sour);函数功能:在dest字符串末尾,追加sour字符串。模拟实现:

4.strcmp
函数声明:int strcmp(const char* str1,const char* str2);函数功能:按字典序从前往后比较两个字符串的的大小;若两字符串相等,则返回0,若str1 > str2 ,则返回大于0的值,若str1 < str2 ,则返回小于0的值。模拟实现:

5.strncpy
函数声明:char* strcpy(char* dest,const char* sour,size_t num);函数功能:与strcpy的区别就是,拷贝num个字符。注意事项:你让拷贝几个就拷贝几个,不自动补'\0';若源头字符串拷贝完了,则会拷贝 '\0' ,直到拷贝数目达到num个。代码案例:
6.strncat
函数声明:char* strcat(char* dest,const char* sour,size_t num);函数功能:与strcat的区别就是,追加num个字符。注意实现:为了保证追加后的字符串能为字符串(以'\0'为结束标志的一组字符)。每次追加完num个字符后,会自动补上'\0'。且num表示的是最大追加的字符个数(当源头字符串追加完之后,无论num是否为0,都将停止追加)。代码案例:
7.strncmp
函数声明:int strcmp(const char* str1,const char* str2,size_t num);函数功能:与strcmp的区别就是,仅比较num个字符。8.strstr
函数声明:char* strstr(const char* str1,const char* str2);函数功能:在字符串str1中搜索为str2的字串;若搜索到,则返回该字串在str1的首字符地址。否则将返回NULL;若str2为空串(长度为0的字符串),则将str1的首字符地址直接返回。模拟实现:

9.strtok
函数声明:char* strtok(char* str1,const char* str2);函数功能:根据str2字符串中的字符,将str1字符串进行切割。函数说明:第⼀个参数指定⼀个字符串,它包含了0个或者多个由str2字符串中⼀个或者多个分隔符分割的标记。第二个参数指向⼀个字符串,定义了⽤作分隔符的字符集合。strtok函数的第⼀个参数不为 NULL时 ,函数将找到str中第⼀个标记,并将其⽤ \0 结尾,strtok函数将保存它在字符串中的位置。strtok函数的第⼀个参数为 NULL 时,函数将在同⼀个字符串中被保存的位置开始,查找下⼀个标strtok函数找到str中的下⼀个标记,并将其⽤ \0 结尾,返回⼀个指向这个标记的指针。注:strtok函数会改变被操作的字符串,所以在使⽤strtok函数切分的字符串⼀般都是临时拷⻉的内容并且可修改。如果字符串中不存在更多的标记,则返回 NULL 指针。代码案例:
10.strerror
函数声明:char * strerror ( int errnum );函数功能:strerror函数可以把参数部分错误码对应的错误信息的字符串地址返回来。函数说明:在不同的系统和C语⾔标准库的实现中都规定了⼀些错误码,⼀般是放在<errno.h>这个头⽂件中说明的,C语⾔程序启动的时候就会使⽤⼀个全⾯的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表⽰没有错误,当我们在使⽤标准库中的函数的时候发⽣了某种错误,就会讲对应的错误码,存放在errno中,⽽⼀个错误码的数字是整数很难理解是什么意思,所以每⼀个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回。代码案例:
11perror
函数声明:void perror( const char *string );函数功能:是strerror与perror等函数功能结合体。代码案例: