字符串与内存函数总结
创始人
2025-05-30 23:44:05

前言:

🍍个人主页🍍:🔜勇敢的小牛儿🚩

🔱推荐专栏🔱: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头文件


 

 一,strlen函数

1.1strlen函数的参数以及返回类型:

1.2strlen函数的返回值:

1.3strlen函数的使用:

#include
#include//头文件
int main() {char arr[20] = "abcdef";int len = strlen(arr);printf("%d\n", len);
}

输出:6

1.4模拟实现:

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

1.5🛑注意事项🛑:

字符数组内必须要有'\0'

二,strcpy函数

 2.1strcpy函数参数及返回类型:

2.2返回值:

 2.3函数的使用:

#include
#include
int main() {char s1[20] = "abc";char s2[20] = "def";strcpy(s1, s2);printf("%s\n", s1);return 0;
}

输出:def 

2.4strcpy函数的模拟实现

char* my_strcpy(char* s1, const char* s2) {assert(s1&&s2);char* start = s1;while (*s1++ = *s2++) {}return start;
}

输出:def  

2.5🛑注意事项🛑:

字符串内必须含有"\0’

空间要足够大,s1要能装得下s2

三,strcat函数

3.1strcat函数的参数以及返回类型:

3.2strcat函数的返回值:

3.3strcat函数的使用:

#include
#include
int main() {char s1[20] = "abc";char s2[20] = "def";strcat(s1, s2);printf("%s\n", s1);return 0;
}

 输出:abcdef

 3.4strcat函数的模拟实现:

char* my_strcat(char* s1, const char* s2) {assert(s1&&s2);char*start = s1;while (*s1) {s1++;}while (*s1++ = *s2++) {}return start;}

输出;abcdef 

3.5🛑注意事项🛑:

1.模拟实现的my_strcat不能自己与自己拼接,比如:my_strcat(s1,s1)

2.传入字符串也必须有'\0'

四,strcmp函数

4.1strcmp函数的参数与返回类型

4.2strcmp函数的返回值:

4.3strcmp函数的使用:

#include
#include
int main() {char s1[] = "abcdef";char s2[] = "abcdg";int ret = strcmp(s1, s2);printf("%d\n", ret);
}

输出:-1 

4.4strcmp函数的模拟实现:

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'

五,有长度限制的函数

5.1函数strncpy,strncat,strncmp:

  

5.2函数的使用:

strncpy:

#include
#include
int main() {char s1[6] = "abcde";char s2[9] = "xxxxxxxx";strncpy(s1, s2, 3);printf("%s\n", s1);return 0;
}

输出:xxxde 

strncat:

#include
#include
int main() {char s1[12] = "abcde";char s2[9] = "xxxxxxxx";strncat(s1, s2, 3);printf("%s\n", s1);return 0;
}

输出:abcdefxxx 

 strncmp:

#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 

5.3🛑注意事项🛑:

注意两个字符数组的空间大小,要确保不要溢出

字符数组要有'\0'

六,strstr函数

strstr函数详细介绍

七,strtok函数-----起分割作用的函数

7.1strtok函数的返回类型以及参数:

7.2strtok函数的返回值:

7.3strtok函数的使用:

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 

 7.4:💖我会再写一篇博客来详细介绍这个strtok函数的💖

八,strerror函数

8.1strerror函数的作用

返回错误码所对应的错误信息

8.2例子 

#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

九,内存函数:memcpy,memmove,memcmp

9.1函数作用:

1.memcpy实现的功能与strcpy的功能一样,不过memcpy函数能够作用到任意类型的字符串中。

2.memmove函数与memcpy函数的功能是类似的。在源空间与目标空间发生重叠时要用memmove而不是memcpy.

3.memcmp与strcmp实现的功能一样。只不过memcmp能够比较的类型更多,但是strcmp能比较的类型只有字符型。

9.2头文件

string.h

相关内容

热门资讯

湖北省全国百强县篮球联赛收官 ... (来源:中国体育报)转自:中国体育报  12月13日,2025年湖北省全国百强县篮球联赛迎来最后一轮...
患儿在宁波大学附属妇女儿童医院... 本文转自【健康宁波】;情况通报2025年11月14日晚,患儿许某某在宁波大学附属妇女儿童医院(以下简...
特朗普各种打压之下,可再生能源...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会!   来源:华尔街见闻...
澳总理召开国家安全委员会紧急会... △当地时间14日傍晚,澳大利亚悉尼邦迪滩发生枪击事件,造成多人死伤总台记者14日获悉,澳大利亚总理阿...
【甘快看·强信心 看发展】甘肃... 【强信心看发展】甘肃新能源年发电量首次突破900亿千瓦时制图:石代学每日甘肃网讯(新甘肃·甘肃日报记...