1.字符分类函数
C语言中有一系列的函数是专门做字符分类的,也就是一个字符是属于什么类型的字符的,这些函数的使用都需要包含一个头文件是ctype.h
这些函数的使用方法非常类似,我们详细介绍一个函数,其他的非常类似
int islower(int c);
islower 是能够判断参数部分的c是否是小写字母的。
通过返回值来说明是否是小写字母,如果是小写字母就返回非0的整数,如果不是小写字母,则返回0.
2.字符转换函数
C语言提供了两个字符转换函数:
int tolower(int c);
int toupper(int c);
上面代码,我们将小写转换成大写,是-32完成的效果,有了转换函数,就可以直接使用tolower函数
#include<stdio.h>
#include<ctype.h>
int main() {
int ret1 = islower('a');
int ret2 = islower('A');
printf("%d\n", ret1);
printf("%d\n", ret2); //写一个代码,将字符串中的小写字母转大写字母,其他字符不变
char arr[] = "I Am a Student";
int i = 0;
while (arr[i] != '\0') {
if (islower(arr[i])) {
arr[i] = toupper(arr[i]);
}
i++;
}
printf("%s\n", arr);
return 0;
}
3.strlen的使用和模拟实现
size_t strlen(const char*str);
- 字符串以'\0'作为结束标志,strlen函数返回的是在字符串中'\0'前面出现的字符个数(不包含'\0')
- 参数指向的字符串必须要以'\0'结束
- 注意函数的返回值为size-t,是无符号的(易错)
- strlen的使用要包含头文件string.h
- •学会模拟实现
size_t无符号整数,strlen("abc")-strlen("abcdef")的到结果也是size_t, -3当成无符号数,结果是非常大的正数
strlen的模拟实现:
方式1:
方式2:
方式3:
4.strcpy的使用和模拟实现
char* strcpy(char *destination,const char*source);
strcpy--string copy字符串拷贝
- 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' 拷⻉到⽬标空间。
- ⽬标空间必须⾜够⼤,以确保能存放源字符串。
- ⽬标空间必须可修改,不能是常量字符串。
- strlen的使用要包含头文件string.h
- 学会模拟实现。
strcpy的模拟实现:
5.strcat的使用和模拟实现
strcat函数用来连接字符串
- Appends a copy of the source string to the destination string. The terminating null characterin destination is overwritten by the first character of source, and a null-character is includedat the end of the new string formed by the concatenation of both in destination.
- 源字符串必须以 '\0' 结束。
- ⽬标字符串中也得有 \0 ,否则没办法知道追加从哪⾥开始。
- ⽬标空间必须有⾜够的⼤,能容纳下源字符串的内容。
- ⽬标空间必须可修改。
- 包含头文件string.h
char* strcat(char *destination,const char*source);
strcat函数的模拟实现:
my_strcat不能实现自己给自己追加
char* my_strcat(char* dest,const char*src) {
assert(dest && src);
char* ret = dest;
while (*dest != '\0') {
dest++;
}
while (*dest++ = *src++) {
;//空语句
}
return ret;
}int main() {
char arr1[20] = "abcdef";
my_strcat(arr1, arr1);
return 0;
}//死循环
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.
- 包含头文件string.h
- 标准规定:
- 第⼀个字符串⼤于第⼆个字符串,则返回⼤于0的数字
- 第⼀个字符串等于第⼆个字符串,则返回0
- 第⼀个字符串⼩于第⼆个字符串,则返回⼩于0的数字
- 那么如何判断两个字符串? ⽐较两个字符串中对应位置上字符ASCII码值的⼤⼩。
strcmp函数的模拟实现 :
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.
- 包含头文件string.h
- 拷⻉num个字符从源字符串到⽬标空间。
- 如果源字符串的⻓度⼩于num,则拷⻉完源字符串之后,在⽬标的后边追加0,直到num个。
8.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 terminatingnull-character is copied.(如果source 指向的字符串的⻓度⼩于num的时候,只会将字符串中到\0 的内容追加到destination指向的字符串末尾)。
- 包含头文件string.h
小练习:
9.strncmp函数的使用
int strncmp(const char* str1,const char* str2,size_t name);
- ⽐较str1和str2的前num个字符,如果相等就继续往后⽐较,最多⽐较num个字⺟,如果提前发现不⼀样,就提前结束,⼤的字符所在的字符串⼤于另外⼀个。如果num个字符都相等,就是相等返回0.
- 包含头文件string.h
10.strstr的使用和模拟实现
char * strstr(const char* str,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 作为结束标志)。
包含头文件string.h
strstr的模拟实现:
char* my_strstr(const char* str1,const char* str2) {
const char* s1 = NULL;
const char* s2 = NULL;
const char* cur = str1;
if (*str2 == '\0') {
return (char*)str1;
}
while (*cur) {
s1 = cur;
s2 = str2;
while (*s1!='\0' && *s2 != '\0' && *s1 == *s2) {
s1++;
s2++;
}
if (*s2 == '\0') {
return cur;
}
cur++;
}
return NULL;
}
int main() {
char arr1[] = "abcdefabcdef";
char arr2[] = "cdef";
char* ret = my_strstr(arr1, arr2);
if (ret == NULL) {
printf("找不到");
}
else{
printf("%s\n", ret);
}
return 0;
}
11.strtok函数的使用
char * strtok(char* str,const char*sep);
- sep参数指向⼀个字符串,定义了⽤作分隔符的字符集合第⼀个参数指定⼀个字符串,它包含了0个或者多个由sep字符串中⼀个或者多个分隔符分割的标记。
- strtok函数找到str中的下⼀个标记,并将其⽤ \0 结尾,返回⼀个指向这个标记的指针。(注:
- strtok函数会改变被操作的字符串,所以在使⽤strtok函数切分的字符串⼀般都是临时拷⻉的内容并且可修改。)
- strtok函数的第⼀个参数不为 NULL ,函数将找到str中第⼀个标记,strtok函数将保存它在字符串中的位置。
- strtok函数的第⼀个参数为 NULL ,函数将在同⼀个字符串中被保存的位置开始,查找下⼀个标记。
- 如果字符串中不存在更多的标记,则返回 NULL 指针。
- 包含头文件string.h
12.strerror函数的使用
char * strerror(int errnum);
strerror 函数可以把参数部分错误码对应的错误信息的字符串地址返回来。
在不同的系统和C语⾔标准库的实现中都规定了⼀些错误码,⼀般是放在 errno.h 这个头⽂件中说明的,C语⾔程序启动的时候就会使⽤⼀个全局的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表⽰没有错误,当我们在使⽤标准库中的函数的时候发⽣了某种错误,就会将对应的错误码,存放在errno中,⽽⼀个错误码的数字是整数很难理解是什么意思,所以每⼀个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回。
包含头文件string.h
也可以了解⼀下 perror 函数,perror函数相当于⼀次将上述代码中的printf("%s\n",strerror(errno));完成了,直接将错误信息打印出来。perror函数打印完参数部分的字符串后,再打印⼀个冒号和⼀个空格,再打印错误信息。