🍍个人主页🍍:🔜勇敢的小牛儿🚩
🔱推荐专栏🔱:C语言知识点
⚠️座右铭⚠️:敢于尝试才有机会
🐒今日鸡汤🐒: A person who has never doubted
the direction and goal of life will never despair. -- Mauriac
一个从不怀疑生活的目标和方向的人绝不会绝望——莫里亚克

目录
前言:
一,strlen函数
1.1strlen函数的参数以及返回类型:
1.2strlen函数的返回值:
1.3strlen函数的使用:
1.4模拟实现:
1.5🛑注意事项🛑:
二,strcpy函数
2.1strcpy函数参数及返回类型:
2.2返回值:
编辑
2.3函数的使用:
2.4strcpy函数的模拟实现
2.5🛑注意事项🛑:
三,strcat函数
3.1strcat函数的参数以及返回类型:
3.2strcat函数的返回值:
3.3strcat函数的使用:
3.4strcat函数的模拟实现:
3.5🛑注意事项🛑:
四,strcmp函数
4.1strcmp函数的参数与返回类型
4.2strcmp函数的返回值:
4.3strcmp函数的使用:
4.4strcmp函数的模拟实现:
五,有长度限制的函数
5.1函数strncpy,strncat,strncmp:
5.2函数的使用:
strncpy:
strncat:
strncmp:
5.3🛑注意事项🛑:
六,strstr函数
七,strtok函数-----起分割作用的函数
7.1strtok函数的返回类型以及参数:
7.2strtok函数的返回值:
7.3strtok函数的使用:
7.4:💖我会再写一篇博客来详细介绍这个strtok函数的💖
八,strerror函数
8.1strerror函数的作用
8.2例子
九,内存函数:memcpy,memmove,memcmp
9.1函数作用:
9.2头文件
#include
#include//头文件
int main() {char arr[20] = "abcdef";int len = strlen(arr);printf("%d\n", len);
}
输出:6
1.4.1计数法:
int my_strlen(char* str) {assert(str);int count = 0;while (*str++) {count += 1;}return count;
}
输出:6
1.4.2递归法:
int my_strlen(char* str) {assert(str);if (*str != '\0')return 1 + my_strlen(str + 1);return 0;}
输出:6
1.4.3指针减指针法:
int my_strlen(char* str) {assert(str);char* start = str;while (*str) {str++;}return str - start;
}
输出:6
字符数组内必须要有'\0'
#include
#include
int main() {char s1[20] = "abc";char s2[20] = "def";strcpy(s1, s2);printf("%s\n", s1);return 0;
}
输出:def
char* my_strcpy(char* s1, const char* s2) {assert(s1&&s2);char* start = s1;while (*s1++ = *s2++) {}return start;
}
输出:def
字符串内必须含有"\0’
空间要足够大,s1要能装得下s2
#include
#include
int main() {char s1[20] = "abc";char s2[20] = "def";strcat(s1, s2);printf("%s\n", s1);return 0;
}
输出:abcdef
char* my_strcat(char* s1, const char* s2) {assert(s1&&s2);char*start = s1;while (*s1) {s1++;}while (*s1++ = *s2++) {}return start;}
输出;abcdef
1.模拟实现的my_strcat不能自己与自己拼接,比如:my_strcat(s1,s1)
2.传入字符串也必须有'\0'
#include
#include
int main() {char s1[] = "abcdef";char s2[] = "abcdg";int ret = strcmp(s1, s2);printf("%d\n", ret);
}
输出:-1
int my_strcmp(const char* s1, const char* s2) {assert(s1&&s2);while (*s1 == *s2) {if (*s1 == '\0')return 0;s1++;s2++;}if (*s1 - *s2 > 0)return 1;elsereturn -1;
}
输出:-1
4.5🛑注意事项🛑
字符串要有'\0'
#include
#include
int main() {char s1[6] = "abcde";char s2[9] = "xxxxxxxx";strncpy(s1, s2, 3);printf("%s\n", s1);return 0;
}
输出:xxxde
#include
#include
int main() {char s1[12] = "abcde";char s2[9] = "xxxxxxxx";strncat(s1, s2, 3);printf("%s\n", s1);return 0;
}
输出:abcdefxxx
#include
#include
int main() {char s1[12] = "abcde";char s2[9] = "abcxxxxx";int ret = strncmp(s1, s2, 3);printf("%d\n", ret);return 0;
}
输出:0
注意两个字符数组的空间大小,要确保不要溢出
字符数组要有'\0'
strstr函数详细介绍
7.3.1:
#include
#include
int main() {char s[] = "Cqin@yeah.net";char buf[20] = { 0 };strcpy(buf, s);const char* p = "@.";char* str1 = strtok(buf, p);printf("%s\n", str1);char* str2 = strtok(NULL, p);printf("%s\n", str2);char* str3 = strtok(NULL, p);printf("%s\n", str3);return 0;
}
输出:Cqin
yeah
net
7.3.2优化:
#include
#include
int main() {char s[] = "Cqin@yeah.net";char buf[20] = { 0 };strcpy(buf, s);const char* p = "@.";char* str = NULL;for (str = strtok(buf, p);str != NULL;str = strtok(NULL, p)) {printf("%s\n", str);}return 0;
}
输出:Cqin
yeah
net
返回错误码所对应的错误信息
#include
#include
#include//errno的头文件
int main() {FILE* pf = fopen("test.p", "r");//文件不存在就返回NULLif (pf == NULL) {printf("打开文件失败,原因是:%s\n", strerror(errno));return 1;}fclose(pf);pf = NULL;return 0;return 0;
}
输出:打开文件失败,原因是:No such file or directory
1.memcpy实现的功能与strcpy的功能一样,不过memcpy函数能够作用到任意类型的字符串中。
2.memmove函数与memcpy函数的功能是类似的。在源空间与目标空间发生重叠时要用memmove而不是memcpy.
3.memcmp与strcmp实现的功能一样。只不过memcmp能够比较的类型更多,但是strcmp能比较的类型只有字符型。
string.h