福州商城网站建设,时代设计网 新网站,路由器怎么做网站,游戏网站织梦模板✨个人主页#xff1a; 熬夜学编程的小林 #x1f497;系列专栏#xff1a; 【C语言详解】 【数据结构详解】 目录 1、字符分类函数 2、字符转换函数 3、strlen的使用和模拟实现 4、strcpy 的模拟实现 5、strcat 的模拟实现 6、strcmp 的模拟实现 7、strncpy 函数的使用 总结… ✨个人主页 熬夜学编程的小林 系列专栏 【C语言详解】 【数据结构详解】 目录 1、字符分类函数 2、字符转换函数 3、strlen的使用和模拟实现 4、strcpy 的模拟实现 5、strcat 的模拟实现 6、strcmp 的模拟实现 7、strncpy 函数的使用 总结 在编程的过程中我们经常要处理字符和字符串为了方便操作字符和字符串C语言标准库中提供了 ⼀系列库函数接下来我们就学习⼀下这些函数。 1、字符分类函数 C语言中有⼀系列的函数是专门做字符分类的也就是⼀个字符是属于什么类型的字符的。 这些函数的使用都需要包含⼀个头文件是 ctype.h int islower ( int c ); islower 是能够判断参数部分的 c 是否是小写字母的。 通过返回值来说明是否是小写字母 如果是小写字母就返回非0的整数如果不是小写字母则返回0。 上述函数比较简单uu们自行验证啦最好通过查库函数的相关知识验证喔~ 练习 写⼀个代码将字符串中的小写字母转大写其他字符不变。 #include stdio.h
#include ctype.h//判断字符函数的头文件
int main ()
{int i 0;char str[] Test String.\n;char c;while (str[i]){c str[i];if (islower(c)) //如果是小写字母则执行c - 32;//大小写之间ASCII码值间隔32,也可以通过两个字符相减即a-A;putchar(c);//输出该字符i;}return 0;
} 2、字符转换函数 C语言提供了2个字符转换函数 int tolower ( int c ); //将参数传进去的⼤写字⺟转⼩写
int toupper ( int c ); //将参数传进去的⼩写字⺟转⼤写 上面的代码我们将小写转大写是 -32 完成的效果有了转换函数就可以直接使用 tolower 函 数。 #include stdio.h
#include ctype.h
int main ()
{int i 0;char str[] Test String.\n;char c;while (str[i]){c str[i];//将该字符放在临时变量中if (islower(c)) //是小写字母则转换c toupper(c);putchar(c);//输出字符i;}return 0;
} 3、strlen的使用和模拟实现 注在模拟实现函数时尽可能的将以下五个点都考虑到。 1.参数顺序 2.const修饰指针(防止指针被修改) 3.函数的功能停止条件 4.assert(对空指针进行判断) 5.函数返回值 size_t strlen ( const char * str ); • 字符串以 \0 作为结束标志strlen函数返回的是在字符串中 \0 前面出现的字符个数不包含 \0 )。 • 参数指向的字符串必须要以 \0 结束。 • 注意函数的返回值为size_t是无符号的 易错 • strlen的使用需要包含头文件 • 学会strlen函数的模拟实现 #include stdio.h
#include string.h//strlen函数需包含的头文件
int main()
{const char* str1 abcdef;const char* str2 bbb;if(strlen(str2)-strlen(str1)0){printf(str2str1\n);} else{printf(srt1str2\n);}return 0;
} strlen的模拟实现 方式1 strlen计算的是传入的指针到\0之间个数我们第一想到的肯定是通过遍历该数组计算字符串长度然后根据上面注意的五个点一步一步进行实现。 首先参数顺序只有一个参数不需要管顺序。 第二个const修饰指针此处只需遍历数组不会修改字符因此可以用const修饰指针。 第三个函数功能是计算字符串长度\0之前的长度。 第四个assert断言传参是指针所以有可能是空指针此处可以进行断言空指针则报错。 第五个返回值位无符号整数C语言提供无符号整数类型size_t此处用int影响也不会很大只是用size_t更加标准准确。 //计数器方式
size_t my_strlen(const char * str)
{int count 0;assert(str);//空指针则报错while(*str)//遍历字符串遇到\0则结束循环{count;str;}return count;//返回大小
} 方式2 递归实现长度1strlen(str1)字符串长度该字符后面字符串的长度。 //不能创建临时变量计数器
size_t my_strlen(const char * str)
{assert(str);//空指针则报错if(*str \0)return 0;elsereturn 1my_strlen(str1);
} 方式3 根据前面指针知识所学指针-指针相差元素个数因此字符串长度可以通过指针-指针计算。 //指针-指针的⽅式
size_t my_strlen(const char *s)
{assert(str);char *p s;while(*p ! ‘\0’ )p;return p-s;
} 4、strcpy 的模拟实现 char* strcpy(char * destination, const char * source ); • Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). 将 source 指向的 C 字符串复制到目标指向的数组中包括终止 null 字符并在该点停止。 • 源字符串必须以 \0 结束。 • 会将源字符串中的 \0 拷贝到目标空间。 • 目标空间 必须足够大以确保能存放源字符串。 • 目标空间必须可修改。 • 学会模拟实现。 strcpy的模拟实现: //1.参数顺序 第一个为目标字符串第二个为原来字符串
//2.const修饰指针 目标需要修改原来不需要修改
//3.函数的功能停⽌条件
//4.assert 两个均不能为空指针
//5.函数返回值 返回目标字符串首地址
//6.题⽬出⾃《⾼质量C/C编程》书籍最后的试题部分
char *my_strcpy(char *dest, const char*src)
{ char *ret dest;assert(dest ! NULL);//为真则不报错assert(src ! NULL);//为真则不报错while((*dest *src)){;}return ret;
} 5、strcat 的模拟实现 • Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination. 将源字符串的副本追加到目标字符串。destination 中的终止 null 字符被 source 的第一个字符覆盖并且在 destination 中由两者串联形成的新字符串的末尾包含一个 null 字符。 • 源字符串必须以 \0 结束。 • 目标字符 串中也得有 \0 否则没办法知道追加从哪里开始。 • 目标 空间必须有足够的大能容纳下源字符串的内容。 • 目标 空间必须可修改。 • 字符串自己给自己追加如何 模拟实现strcat函数 char *my_strcat(char *dest, const char*src)
{char *ret dest;assert(dest ! NULL);assert(src ! NULL);while(*dest){dest;}while((*dest *src)){;}return ret;
} 6、strcmp 的模拟实现 • This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. 此函数开始比较每个字符串的第一个字符。如果它们相等则继续往下比较直到字符不同或终止达到 null-character。 • 标准规定 ◦ 第⼀个字符串大于第⼆个字符串则返回大于0的数字 ◦ 第⼀个字符串等于第⼆个字符串则返回0 ◦ 第⼀个字符串小于第⼆个字符串则返回小于0的数字 ◦ 那么如何判断两个字符串 比较两个字符串中对应位置上字符ASCII码值的大小。 strcmp函数的模拟实现 int my_strcmp (const char * str1, const char * str2)
{int ret 0 ;assert(src ! NULL);assert(dest ! NULL);while(*str1 *str2){if(*str1 \0)//两个字符串相等且遇到\0即字符串相等返回0return 0;str1;str2;}return *str1-*str2;//不相等返回差值0即str1大0即str2大
} 7、strncpy 函数的使用 char * strncpy ( char * destination, const char * source, size_t num ); • Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it. 将源的前 num 个字符复制到目标。如果源 C 字符串的末尾由 null 字符表示在复制 num 个字符之前找到destination 用零填充直到总共写入了 num 个字符。 • 拷贝num个字符从源字符串到目标空间。 • 如果源字符串的长度小于num则拷贝完源字符串之后在目标的后边追加0直到num个。 #include stdio.h
#include string.h//strncpy函数需包含的头文件
int main()
{char str1[]abcdeef;char str2[20]{0};strncpy(str2,str1,5);printf(%s\n,str2);return 0;
} 总结 本篇博客就结束啦谢谢大家的观看如果公主少年们有好的建议可以留言喔谢谢大家啦