C语言第二十五弹---字符函数和字符串函数(上)

✨个人主页:熬夜学编程的小林

💗系列专栏: 【C语言详解】 【数据结构详解】
目录

1、字符分类函数

2、字符转换函数

3、strlen的使用和模拟实现

[4、strcpy 的模拟实现](#4、strcpy 的模拟实现)

[5、strcat 的模拟实现](#5、strcat 的模拟实现)

[6、strcmp 的模拟实现](#6、strcmp 的模拟实现)

[7、strncpy 函数的使用](#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("str2>str1\n");
 } 
 else
 {
 printf("srt1>str2\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:
递归实现,长度=1+strlen(str+1),字符串长度=该字符+后面字符串的长度。

//不能创建临时变量计数器
size_t my_strlen(const char * str)
{
 assert(str);//空指针则报错
 if(*str == '\0')
 return 0;
 else
 return 1+my_strlen(str+1);
}

方式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'即字符串相等,返回0
 return 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;
}

总结

本篇博客就结束啦,谢谢大家的观看,如果公主少年们有好的建议可以留言喔,谢谢大家啦!

相关推荐
ignativs amor20 分钟前
Python redis 安装和使用介绍
开发语言·redis·python
你可以自己看23 分钟前
Python在数据科学与机器学习中的应用
开发语言·python·机器学习
Jacky-YY23 分钟前
教你如何在Java中操作Redis
java·开发语言·redis
红花与香菇2____1 小时前
【学习笔记】STM32F407探索者HAL库开发(四)F103时钟系统配置
c语言·笔记·stm32·单片机·嵌入式硬件·学习
大白的编程日记.1 小时前
【C++笔记】C++编译器拷贝优化和内存管理
java·开发语言·c++·笔记
JovaZou1 小时前
[Python学习日记-26] Python 中的文件操作
开发语言·python·学习
CV金科1 小时前
蓝桥杯-基于STM32G432RBT6的LCD进阶(LCD界面切换以及高亮显示界面)
c语言·stm32·单片机·嵌入式硬件·蓝桥杯
¥ 多多¥3 小时前
数据结构:内存的使用
linux·c语言·开发语言·数据结构
EPSDA3 小时前
Java的IO流(二)
java·开发语言
小灰灰爱代码3 小时前
C++——将数组a[5]={-1,2,9,-5,7}中小于0的元素置成0。并将其结果输出(要求:用数组名作为函数的参数来实现)
数据结构·c++·算法