网站判断手机跳转代码,游戏seo推广,做网站和软件有区别吗,36岁了初中毕业想提升学历这是最好的时代#xff0c;这是最坏的时代#xff0c;我们一无所有#xff0c;我们巍然矗立 本文由睡觉待开机原创#xff0c;未经允许不得转载。 本内容在csdn网站首发 欢迎各位点赞—评论—收藏 如果存在不足之处请评论留言#xff0c;共同进步#xff01; 系列文章目录… 这是最好的时代这是最坏的时代我们一无所有我们巍然矗立 本文由睡觉待开机原创未经允许不得转载。 本内容在csdn网站首发 欢迎各位点赞—评论—收藏 如果存在不足之处请评论留言共同进步 系列文章目录
注由于系列文章没有完成系列文章目录暂时欠缺。 C语言字符串函数及其模拟实现本文章 C语言中动态内存管理 文章目录 系列文章目录前言1.字符分类函数2.字符转换函数3.strlen求字符串长度函数3.1strlen函数的使用(头文件string.h)3.2strlen函数的模拟实现 4.strcpy字符串拷贝函数4.1strcpy函数的使用4.2strcpy函数的模拟实现 5.strcat函数5.1strcat函数的使用5.2strcat函数的模拟实现 6.strcmp字符串比较函数6.1strcmp函数的使用6.2strcmp函数的模拟实现 7.strncpy,strncat,strncmp函数7.1strncpystrncatstrncmp7.2strcpystrcatstrcmp与strncpystrncatstrncmp的区别7.2.1strncpy函数7.2.1strncat函数7.3strncmp函数 8.strstr函数8.1strstr函数的使用8.2strstr函数的模拟实现 9.strtok函数9.1strtok函数的基本语法9.2strtok函数的使用代码 10.strerror与str10.1strerror10.1.1strerror函数的基本语法规则10.1.2sterror函数的使用代码 10.2perror函数10.2.1perror函数的基本语法头文件stdio.h10.2.2perror函数的使用举例 总结 前言
字符是C语言中整形家族的一大特殊群体其本质是ASCII码值进行转换的其相关的常见库函数会在编写代码时提供一些便利下面来简单看一下 1.字符分类函数
字符分类函数是对于全体字符进行分类的。 我们以isdigit函数语法为例其他都是基本相同的
2.字符转换函数
常用的两个字符转换函数
3.strlen求字符串长度函数
3.1strlen函数的使用(头文件string.h) #includestdio.h
#includestring.h
//strlen函数返回值是size_t容易误用
int main()
{if (strlen(abc) - strlen(abcdef) 0)//易错3-6printf(surprise\n);elseprintf(normal\n);return 0;
}3.2strlen函数的模拟实现
strlen函数实现有多种方式下面简单介绍几种思路
//strlen函数的模拟实现
size_t my_strlen1(char* str)
{size_t count 0;while (*str){count;}return count;
}
size_t my_strlen2(char* str)
{char* start str;while (*str){;}return str - start - 1;
}
size_t my_strlen3(char* str)
{if (!*str){return 0;}elsereturn 1 my_strlen3(str);
}
int main()
{char str[] abcdef;size_t result1 my_strlen1(str);size_t result2 my_strlen2(str);size_t result3 my_strlen3(str);printf(result of way1 %zd\n, result1);printf(result of way2 %zd\n, result2);printf(result of way3 %zd\n, result3);return 0;
}4.strcpy字符串拷贝函数
4.1strcpy函数的使用
功能strcpy是将source字符串中的内容拷贝到destion中去包括\0 返回值说明如果拷贝成功返回destion的地址如果拷贝失败返回NULL
4.2strcpy函数的模拟实现
//strcpy函数的模拟实现
char* my_syrcpy(char* des, const char* src)
{char* start des;while (*des *src){;}return start;
}
int main()
{char des[100] {0};char src[] hello the world;//char* result1 strcpy(des,src);char* result2 my_syrcpy(des,src);//printf(result of library function is: %s\n, result1);printf(result of custom function is: %s\n, result2);return 0;
}5.strcat函数
5.1strcat函数的使用
功能将source中的字符串追加到destion中字符串的后面去。 返回值说明如果追加成功返回destion的地址如果拷贝失败返回NULL
5.2strcat函数的模拟实现
//strcat函数的模拟实现
char* my_strcat(char* str,const char* src)
{char* start str;while (*str){;}str--;while (*str *src){;}return start;
}
int main()
{char str[100] hello ;char src[] the world;//char* result1 strcat(str,src);char* result2 my_strcat(str, src);//printf(result1 of library function is : %s\n, result1);printf(result2 of custom function is : %s\n, result2);return 0;
}6.strcmp字符串比较函数
6.1strcmp函数的使用 6.2strcmp函数的模拟实现
//strcmp函数的模拟实现
int my_strcmp(char* str1, char* str2)
{while (*str1 *str2){if (*str1 \0){return 0;}str1;str2;}if (*str1 *str2)return 1;elsereturn -1;
}
int main()
{char str1[] abcdef;char str2[] abcq;int result my_strcmp(str1,str2);if (result 0)printf(str1 str2\n);else if (result 0)printf(str1 str2\n);elseprintf(str1 str2\n);return 0;
}7.strncpy,strncat,strncmp函数
7.1strncpystrncatstrncmp
strncpystrncatstrncmp这三个函数基本与strcpystrcatstrcmp函数语法基本是一致的不过多了个参数用来控制拷贝几个追加几个比较几个字符相对strcpystrcatstrcmp函数而言更加安全一些。这里不多赘述。
我们举一个例子说明这三个的基本语法
7.2strcpystrcatstrcmp与strncpystrncatstrncmp的区别
7.2.1strncpy函数
stencpy拷贝不会刻意加上\0,把原函数全部拷贝了如果还达不到程序员设置最大字符限制补\0
7.2.1strncat函数
追加完字符串后会主动追加一个\0保证目标字符串是一个字符串。也就是说他会追加n-1个字符还有1个’\0’。
不会超过原字符串的长度去追加。
7.3strncmp函数
这个就是限制了比较的长度而已目前没发现什么区别。
8.strstr函数
8.1strstr函数的使用
strstr函数是干什么的 功能strstr函数是用来在一个字符串中找另一个字符串的第一次出现的地址的。 strstr函数的使用代码一览
int main()
{char destion[] abcabbbbcd;char source[] bcd;char* result1 strstr(destion, source);//char* result2 my_strstr(destion, source);printf(the result of standard library is : %s, result1);//printf(the result of myself way is : %s, result2);return 0;
}8.2strstr函数的模拟实现
char* my_strstr(const char* destion,const char* source)
{char* cur destion;char* s1 NULL;char* s2 NULL;//特殊情况直接让我找个\0,我直接给他把目标字符串的地址给他返回去if (*source \0){return destion;}while (*cur){s1 cur;//每次cur变化时候把s1初始化为cur开始的地方s2 source;//每次cur变化的时候把s2初始化为source开始的地方while (*s1 *s2 *s1 *s2){s1;s2;}if (*s2 \0){return cur;}cur;}return NULL;}
int main()
{char destion[] abcabbbbcd;char source[] bcd;//char* result1 strstr(destion, source);char* result2 my_strstr(destion, source);//printf(the result of standard library is : %s, result1);printf(the result of myself way is : %s, result2);return 0;
}9.strtok函数
9.1strtok函数的基本语法
首先第一个问题哈strtok函数是干啥的下面来揭示strtok函数的基本语法规则
9.2strtok函数的使用代码
//strtok函数的使用
int main()
{char str1[] zhangsan/163qq.com;char str2[] /.;char* s1 strtok(str1, str2);printf(%s\n, s1);s1 strtok(NULL, str2);printf(%s\n, s1);s1 strtok(NULL, str2);printf(%s\n, s1);s1 strtok(NULL, str2);printf(%s\n, s1);return 0;
}代码结果 这样显然是可以使用strtok函数的不过需要调用四次是比较麻烦的一般我们可以结合for循环进行使用
//strtok函数结合for循环使用
int main()
{char str1[] zhangsan/163qq.com;char str2[] /.;char* p NULL;for (p strtok(str1, str2); p ! NULL; p strtok(NULL, str2)){printf(%s\n, p);}return 0;
}10.strerror与str
10.1strerror
10.1.1strerror函数的基本语法规则 10.1.2sterror函数的使用代码
int main()
{int i 0;for (i 0; i 10; i){printf(the %d error is :%s\n,i, strerror(i));}return 0;
}
其结果为
这是0到10错误码翻译对应的信息。 一般我们是这样用的
#include stdio.h
#include string.h
#include errno.h
int main()
{FILE* pFile;pFile fopen(unexist.ent, r);if (pFile NULL)printf(Error opening file unexist.ent: %s\n, strerror(errno));return 0;
}10.2perror函数
10.2.1perror函数的基本语法头文件stdio.h
相对于strerror函数来说更加直接
10.2.2perror函数的使用举例
#include stdio.h
#include string.h
#includeerrno.h
int main()
{FILE* pFile;pFile fopen(unexist.ent, r);if (pFile NULL){//可以放开注释对比一下//printf(Error opening file unexist.ent of strerror: %s\n, strerror(errno));perror(Error opening file unexist.ent of perror);}return 0;
}总结
字符串函数是我们C语言常用的针对字符串操作的函数有利于加深对字符串的理解。 感谢您阅读本文。如果您有任何问题或需要进一步了解请随时联系我。祝您一切顺利