目录
1 用于处理字符的部分函数
1.1 判定字符类别的函数
以下是用来判断字符类别的函数,要想使用它们,需要包含头文件: <ctype.h>
| 函数名 | 功能 |
|---|---|
| iscntrl | 判定是否为控制字符,是返回非0数字,不是返回0,以下同理 |
| isspace | 判定是否为空白字符 |
| isdigit | 判定是否为十进制数字 0~9 |
| isxdigit | 判定是否为十六进制数字 |
| islower | 判定是否为小写字母 a~z |
| isupper | 判定是否为大写字母 A~Z |
| isalpha | 判定是否为字母 a~z A~Z |
| isalnum | 判定是否为数字或字母 0~9 a~z A~Z |
| ispunct | 判定是否为标点符号 |
| isgraph | 判定是否为图形字符 |
| isprint | 判定是否为可打印字符 |
1.2 转换大小写函数
以下是用来字符大小写转换的函数,也需要包含头文件: <ctype.h>
| 函数名 | 功能 |
|---|---|
| islower | 用于将大写字母转换为小写字母 |
| isupper | 用于将小写字母转换为大写字母 |
2 用于处理字符串的部分函数
以下提到的函数都需要包含头文件:<string.h>
2.1 strlen
strlen 的函数声明如下:
size_t strlen ( const char * str );
该函数会从 str 指针指向的位置开始遍历字符串,对字符的数量进行统计,直到遇到 ‘\0’ 为止,最终返回字符串的长度
注意事项:
- strlen 在计算字符串的长度时,不会将 ‘\0’ 统计在内
- strlen 要统计的字符串需要用 ‘\0’ 进行结尾,否则会产生随机值
- strlen 的返回值是 size_t 类型,也就是无符号类型
使用例
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "abcdef";
printf("%d", strlen(s));
return 0;
}
画图理解

strlen 的模拟实现
//1.使用计数器
size_t my_strlen(const char* s)
{
assert(s != NULL);
size_t count = 0;
while (*s != '\0') //遍历直到字符串末尾
{
count++;
s++;
}
return count;
}
//2.使用指针-指针返回指针之间的元素个数
size_t my_strlen(const char* s)
{
assert(s != NULL);
char* t = s;
while (*t != '\0')
{
t++;
}
return t - s;
}
//3.递归
//要算'abcdef'的长度,那么就是算1 + 'bcdef'的长度
//要算'bcdef'的长度,就是算1 +'cdef'的长度
//....
size_t my_strlen(const char* s)
{
if (*s == '\0')
return 0;
else
return 1 + my_strlen(++s);
}
2.2 strcpy
strcpy 的函数声明如下:
char * strcpy ( char * destination, const char * source );
该函数会将 source 所指向的字符串(源字符串)复制到 destination 所指向的字符串内(目标字符串)
注意事项:
- 目标字符串应为可修改的字符串,不可以是常量字符串这种无法修改的字符串
- 目标字符串需要有足够的空间放入源字符串
- 源字符串需要有 ‘\0’ 结尾
- 复制时,会将源字符串中的 ‘\0’ 也复制到目标字符串中
使用例
#include <string.h>
int main()
{
char s1[] = "abcd";
char s2[] = "xxx";
strcpy(s1, s2);
return 0;
}
画图理解

strcpy 的模拟实现
char* my_strcpy(char* destination, const char* source)
{
assert(source && destination);
char* tmp = destination; //因为destination会被更改,所以先保存目标字符串首地址,方便后面返回
//进行复制
//*destination++ = *source++ 简化代码
//放在while循环判断条件内,做到复制'\0'
while (*destination++ = *source++);
return tmp;
}
2.3 strcat
strcat 的函数声明如下:
char * strcat ( char * destination, const char * source );
该函数会将 source 所指向的字符串的内容附加到 destination 所指向的字符串的末尾(\0及之后)
注意事项:
- 在进行附加时,要确保目标字符串是可修改的,不可以是常量字符串
- 目标字符串应包括 ‘\0’ ,否则无法知道开始的位置
- 源字符串应包括 ‘\0’ ,否则不知道源字符串到哪里结束
- 目标字符串的空间需够容纳追加内容
使用例
#include <string.h>
int main()
{
char s1[5] = "ab";
char s2[] = "xx";
strcat(s1, s2);
return 0;
}
画图理解

strcat 的模拟实现
char* my_strcat(char* destination, const char* source)
{
assert(destination && source);
char* tmp = destination;
//找到目标字符串的末尾
while (*destination != '\0')
destination++;
//追加源字符串到目标字符串后面
while (*destination++ = *source++);
return tmp;
}
2.4 strcmp
strcmp 的函数声明如下:
int strcmp ( const char * str1, const char * str2 );
该函数用于比较 str1 和 str2 所指向的字符串,比较时按照每一对字符的ASCII码进行比较
函数返回值
| 比较结果 | 返回值 |
|---|---|
| 1串 > 2串 | >0 |
| 1串 = 2串 | =0 |
| 1串 < 2串 | <0 |
使用例
int main()
{
char s1[5] = "ab";
char s2[] = "xx";
if (strcmp(s1, s2) == 0)
{
printf("字符串相等");
}
else
{
printf("字符串不相等");
}
return 0;
}
画图理解

strcmp 的模拟实现
int my_strcmp(const char* s1, const char* s2)
{
assert(s1 && s2);
while (*s1 == *s2)
{
if (*s1 == '\0') //s1指向的是\0,s2指向的也是\0,说明两个字符串都遍历完,并且相等
{
return 0;
}
s1++;
s2++;
}
//有不相等的字符,返回字符的差值
return *s1 - *s2;
}
2.5 strncpy
strncpy 的函数声明如下:
char * strncpy ( char * destination, const char * source, size_t num );
该函数会将 source 所指向位置的后 num 个字节复制到 destination 所指向位置的后 num 个字节
注意事项:
- strncpy 在进行复制时,如果复制的字符数 < num, 那么会在后方补充 ‘\0’ ,直到字符数等于 num 个
- strncpy 在进行复制时,如果复制的字符数 = num, 那么不会在后方补充 ‘\0’
使用例
#include <string.h>
int main()
{
char s1[5] = "ab";
char s2[] = "xx";
strncpy(s1, s2, 2);
return 0;
}
画图理解

2.6 strncat
strncat 的函数声明如下:
char * strncat ( char * destination, const char * source, size_t num );
该函数会将 source 所指向位置的后 num 个字节附加到 destination 所指向位置的后 num 个字节
注意事项:
- strncat 在进行附加时,复制的字符够 num 个字节,则会在末尾处添加 ‘\0’ 字符
- strncat 在进行附加时,复制的字符不够 num 个字节,则会将源字符串中直到 ‘\0’ 为止的所有字符附加到目标字符串内并添加一个 ‘\0’ 字符
使用例:
#include <string.h>
int main()
{
char s1[5] = "ab";
char s2[] = "xx";
strncat(s1, s2, 2);
return 0;
}
画图理解

2.7 strncmp
strncmp 的函数声明如下:
int strncmp ( const char * str1, const char * str2, size_t num );
该函数会对比 str1 和 str2 指向的后 num 个字节的内容
函数返回值
| 比较结果 | 返回值 |
|---|---|
| 1串 > 2串 | >0 |
| 1串 = 2串 | =0 |
| 1串 < 2串 | <0 |
使用例
#include <string.h>
int main()
{
char s1[5] = "ab";
char s2[] = "xx";
strncmp(s1, s2, 2);
return 0;
}
画图理解

2.8 strstr
strstr 的函数声明如下:
const char * strstr ( const char * str1, const char * str2 );
该函数会在 str1 指向的字符串内中查找 str2 指向的字符串 ,若找到了str2指向的字符串,则会返回该串在 str1 指向的字符串内的首地址
使用例
#include <string.h>
int main()
{
char s1[5] = "abcde";
char s2[] = "cde";
char* ret = strstr(s1, s2);
return 0;
}
画图理解

模拟实现
char* my_strstr(const char* s1, const char* s2)
{
assert(s1 && s2);
const char* str2 = s2; //str2用来记录s2开始匹配的位置
const char* cur = s1; //cur用来记录s1开始匹配的位置
if (*s2 == '\0') //要查找的是空串
return (char*)s1;
while (*cur != '\0') //s1未遍历完,继续进行比较
{
s1 = cur;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2) //两个字符相等,s1和s2继续比较
{
s1++;
s2++;
}
if (*s2 == '\0') //s2指向\0,说明s2遍历结束,在s1内找到了s2
{
return (char*)cur; //返回s2在s1内出现的起始地址
}
cur++;//不相等从s1的下一个位置重新比较
}
return NULL; //s1遍历完成还没有找到s2,查找失败
}

1030

被折叠的 条评论
为什么被折叠?



