字符分类函数 / 字符转换函数

目录

1. 字符分类函数

2. 字符转换函数

二、他们都头文件都为#lnclude

 ① strlen的使用和模拟实现

举例strlen函数的使用:

strlen的模拟实现:

方式1:计数器方式

方式2:指针-指针的方式

方法三: 递归 - 不能使度用临时变量

 ② strcpy 的使用和模拟实现

 ③ strcat 的使用和模拟实现

 ④ strcmp 的使⽤和模拟实现

 ⑤ strstr 的使用和模拟实现

④ strtok 函数的使用

④ strerror 函数的使用

① strncpy 函数的使用

② strncat 函数的使用

③strncmp函数的使用


1. 字符分类函数

C语⾔中有⼀系列的函数是专⻔做字符分类的,也就是⼀个字符是属于什么类型的字符的。
这些函数的使⽤都需要包含⼀个头⽂件是 ctype.h
函数如果他的参数符合下列条件就返回真
   iscontrl任何控制字符
 isspace空白字符:空格‘’,换页‘f’ ,换行'n',回车‘r’  ,制表符'\t'或者垂直制表符'\v'
 isdigit十进制数字‘0’~‘9’字符
 isxdigit十六进制数字,包括所有十进制数字字符,小写字母a~f,大写字母A~F
 islower小写字母a~z
 isupper大写字母A~Z
 isalpha字母a~z或A~Z
 isalnum字母或者数字,a~z,A~Z,0~9
 ispunct标点符号,任何不属于数字或者字母的图形字符 (可打印)
 isgraph任何图形字符
 isprint任何可打印字符,包括图形字符和空白字符

这些函数都很类似,比如:

 int islower ( int c );
islower 是能够判断参数部分的 c 是否是⼩写字⺟的。
通过返回值来说明是否是⼩写字⺟,如果是⼩写字⺟就返回⾮0的整数,如果不是⼩写字⺟,则返回0。
举例:
写⼀个代码,将字符串中的⼩写字⺟转⼤写,其他字符不变。
#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;
 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;
}

二、他们都头文件都为#lnclude <string.h>

 ① strlen的使用和模拟实现

 size_t strlen ( const char * str );
字符串以 '\0' 作为结束标志 ,strlen函数返回的是在字符串中 '\0' 前⾯出现的字符个数(不包 含 '\0' )。
参数指向的字符串必须要以 '\0' 结束。
注意函数的返回值为size_t,是⽆符号的( 易错 )
strlen的使⽤需要包含头⽂件
学会strlen函数的模拟实现

举例strlen函数的使用:

注意:
表达式中的比较值是什么类型,如无符号整型与整型比较,结果会不同:

strlen的模拟实现

方式1计数器方式
//计数器⽅式
int my_strlen(const char * str)//const确保str里面储存的内容不在接下来的过程中被改变
{
 int count = 0;
 assert(str);//断言一下,保证指针有效性
 while(*str)//也可以写成(*str!='0')
 {
 count++;
 str++;
 }
 return count;
}
方式2:指针-指针的方式
//指针-指针的⽅式
int my_strlen(const char *s)
{
 assert(str);
 char *p = s;
 while(*p != ‘\0’ )
 p++;
 return p-s;
}
方法三: 递归 - 不能使度用临时变量

 写一个函数,不能使度用临时变量,求字符串长

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

 ② 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).
源字符串必须以 '\0' 结束。
会将源字符串中的 '\0' 拷⻉到⽬标空间。
⽬标空间必须⾜够⼤,以确保能存放源字符串。
⽬标空间必须可修改。
学会模拟实现。

strcpy函数解释和注意:

从arr1的\0做起始位置,arr2的\0也要拷贝过去

缺少\0,会无法停止

a
常量字符串,不能修改
目标空间必须是可修改的
strcpy的模拟实现:
//1.参数顺序
2 //2.函数的功能,停⽌条件
3 //3.assert
4 //4.const修饰指针
5 //5.函数返回值
6 //6.题⽬出⾃《⾼质量C/C++编程》书籍最后的试题部分
7 char *my_strcpy(char *dest, const char*src)
8 { 
9 char *ret = dest;
10 assert(dest != NULL);
11 assert(src != NULL);
12 
13 while((*dest++ = *src++))
14 {
15 ;
16 }
17 return ret;
18 }
strcpy函数模拟解释和注意

 ③ 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.
源字符串必须以 '\0' 结束。
⽬标字符串中也得有 \0 ,否则没办法知道追加从哪⾥开始。
⽬标空间必须有⾜够的⼤,能容纳下源字符串的内容。
⽬标空间必须可修改。
字符串⾃⼰给⾃⼰追加,如何?

strcat函数简单介绍:

模拟实现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;
}

 1. 找到目标空间的\0;

 2. 拷贝;

 ④ 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.
标准规定:
• 第⼀个字符串⼤于第⼆个字符串,则返回⼤于0的数字
• 第⼀个字符串等于第⼆个字符串,则返回0
• 第⼀个字符串⼩于第⼆个字符串,则返回⼩于0的数字
• 那么如何判断两个字符串? ⽐较两个字符串中对应位置上字符ASCII码值的⼤⼩。
strcmp函数的模拟实现:
注意:要加 #define _CRT_SECURE_NO_WARNINGS 1                                                        
                                                不然会报错                                                                                
int my_strcmp (const char * str1, const char * str2)
{
 int ret = 0 ;
 assert(src != NULL);
 assert(dest != NULL);
 while(*str1 == *str2)
 {
 if(*str1 == '\0')
 return 0;
 str1++;
 str2++;
 }
 return *str1-*str2;
}

三种情况: 1. 字符串大于

                   2. 字符串等于

                   3. 字符串小于

 ⑤ strstr 的使用和模拟实现

strstr 的使用
 char * strstr ( const char * str1, const char * str2);
•  Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. (函数返回字符串str2在字符串str1中第⼀次出现的位置)。
•  The matching process does not include the terminating null-characters, but it stops there.(字符串的⽐较匹配不包含 \0 字符,以 \0 作为结束标志)。
strstr的模拟实现:
char * strstr (const char * str1, const char * str2)
{
 char *cp = (char *) str1;
 char *s1, *s2;
 if ( !*str2 )
 return((char *)str1);
 while (*cp)
 {
 s1 = cp;
 s2 = (char *) str2;
 while ( *s1 && *s2 && !(*s1-*s2) )
 s1++, s2++;
 if (!*s2)
 return(cp);
 cp++;
 }
 return(NULL);
}

模拟实现的解释:

当然我们可以有自己的理解,设计成不一样的

④ strtok 函数的使用

a

char * strtok ( char * str, const char * sep);
•   sep参数指向⼀个字符串,定义了⽤作分隔符的字符集合
•   第⼀个参数指定⼀个字符串,它包含了0个或者多个由sep字符串中⼀个或者多个分隔符分割的标记。
•   strtok函数找到str中的下⼀个标记,并将其⽤ \0 结尾,返回⼀个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使⽤strtok函数切分的字符串⼀般都是临时拷⻉的内容并且可修改。)
•   strtok函数的第⼀个参数不为 NULL ,函数将找到str中第⼀个标记,strtok函数将保存它在字符串中的位置。
•   strtok函数的第⼀个参数为 NULL ,函数将在同⼀个字符串中被保存的位置开始,查找下⼀个标记。
•   如果字符串中不存在更多的标记,则返回 NULL 指针。

④ strerror 函数的使用

a

char * strerror ( int errnum );
strerror函数可以把参数部分错误码对应的错误信息的字符串地址返回来。
在不同的系统和C语⾔标准库的实现中都规定了⼀些错误码,⼀般是放在 errno.h 这个头⽂件中说明的,C语⾔程序启动的时候就会使⽤⼀个全⾯的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表⽰没有错误,当我们在使⽤标准库中的函数的时候发⽣了某种错误,就会讲对应的错误码,存放在errno中,⽽⼀个错误码的数字是整数很难理解是什么意思,所以每⼀个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回。
#include <errno.h>
#include <string.h>
#include <stdio.h>

 //我们打印⼀下0~10这些错误码对应的信息
int main()
{
 int i = 0;
 for (i = 0; i <= 10; i++) {
 printf("%s\n", strerror(i));
 }
 return 0;
}
在Windows11+VS2022环境下输出的结果如下:
No error
Operation not permitted
No such file or directory
No such process
Interrupted function call
Input/output error
No such device or address
Arg list too long
Exec format error
Bad file descriptor
No child processes

运行程序:

注意:

C语言是可以进来文件操作的

打开文件:

FILE* pf = fopen(...)

如果文件打开成功,则返回一个地址

如果文件打开失败,则返回NULL

errno_

举例:
#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;
}
输出:
1 Error opening file unexist.ent: No such file or directory
也可以了解⼀下perror函数,perror 函数相当于⼀次将上述代码中的第9⾏完成了,直接将错误信息打印出来。perror函数打印完参数部分的字符串后,再打印⼀个冒号和⼀个空格,再打印错误信息。
举例:
         
#include <stdio.h>
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 int main ()
5 {
6 FILE * pFile;
7 pFile = fopen ("unexist.ent","r");
8 if (pFile == NULL)
9 perror("Error opening file unexist.ent");
10 return 0;
11 }
输出:
Error opening file unexist.ent: No such file or directory

① 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个字符从源字符串到⽬标空间。
如果源字符串的⻓度⼩于num,则拷⻉完源字符串之后,在⽬标的后边追加     0,直到num个。

*不会拷贝\0

num大于源字符串长度  会追加\0

a

② strncat 函数的使用

char * strncat ( char * destination, const char * source, size_t num );

•  Appends the first num characters of source to destination, plus a terminating null-character. (将source指向字符串的前num个字符追加到destination指向的字符串末尾,再追加⼀个 \0 符)。
•  If the length of the C string in source is less than num, only the content up to the terminating  null-character is copied.(如果source 指向的字符串的⻓度⼩于num的时候,只会将字符串中到 \0 的内容追加到destination指向的字符串末尾)。
/* strncat example */
#include <stdio.h>
#include <string.h>
int main ()
{
 char str1[20];
 char str2[20];
 strcpy (str1,"To be ");
 strcpy (str2,"or not to be");
 strncat (str1, str2, 6);
 printf("%s\n", str1);
 return 0;
}

指定nun追加完。再追加一个\0

从目标字符串的第一个\0位置开始追加

指向的字符串的⻓度⼩于num的时候,只会将字符串中到\0 的内容追加到destination指向的字符串末尾

strncmp函数的使用

a
int strncmp ( const char * str1, const char * str2, size_t num );
⽐较str1和str2的前num个字符,如果相等就继续往后⽐较,最多⽐较num个字⺟,如果提前发现不一样,就提前结束,⼤的字符所在的字符串⼤于另外⼀个。如果num个字符都相等,就是相等返回0.

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚尚.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值