赞
踩
目录
1.strlen用来计算字符串的长度(该长度不包括'\0')
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr[]="abcdef";
- int ret=strlen(arr);
- printf("%d\n",ret);
- return 0;
- }
运行结果:
模拟实现strlen
- #include<stdio.h>
- #include<assert.h>
- size_t my_strlen(const char* arr)
- {
- assert(arr);//断言用来判断接收的arr是否为NULL
- int count = 0;
- while (*arr++)//当*arr不等于'\0'时继续往下计算
- {
- count++;
- }
- return count;
- }
- int main()
- {
- char arr[] = "abcdef";
- int ret = my_strlen(arr);//将arr传过去,传的是数组首元素的地址
- printf("%d\n", ret);
- return 0;
- }
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr1[] = "abcdef";
- char arr2[10] = { 0 };
- strcpy(arr2, arr1);
- printf("%s\n", arr2);
- return 0;
- }
运行结果:
模拟实现strcpy
- #include<stdio.h>
- #include<assert.h>
- char* my_strcpy(char* dest, const char* str)
- {
- assert(dest && str);
- char* ret = dest;//记录dest的起始位置
- while (*dest++ = *str++)//当*str不等于'\0'时一直将现在的值赋值给dest
- {
- ;
- }
- return ret;//函数返回类型为char*用ret返回
- }
- int main()
- {
- char arr1[] = "abcdef";
- char arr2[10] = { 0 };
- my_strcpy(arr2, arr1);
- printf("%s\n", arr2);
- return 0;
- }
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr1[10] = "abcd";
- char arr2[] = "efg";
- strcat(arr1, arr2);
- printf("%s\n", arr1);
- return 0;
- }
运行结果:
模拟实现strcat
- #include<stdio.h>
- #include<assert.h>
-
- char* my_strcat(char* dest, const char* str)
- {
- assert(dest && str);
- char* ret = dest;
- while (*dest)//找到'\0'的位置
- {
- dest++;
- }
- while (*dest++ = *str++)//将字符赋值给目标
- {
- ;
- }
- return ret;
- }
- int main()
- {
- char arr1[10] = "abcd";
- char arr2[] = "efg";
- my_strcat(arr1, arr2);
- printf("%s\n", arr1);
- return 0;
- }
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr1[] = "zhangsan";
- char arr2[] = "lisi";
- int ret = strcmp(arr1, arr2);
- printf("%d\n",ret);
- return 0;
- }
运行结果:
模拟实现strcmp
string1小于string2时返回值小于0
string1等于string2时返回值等于0
string1大于string2时返回值大于0
该比较方法为比较等同位置的字符的ASCLL值的大小
若相同的话比较下一个字符的ASCLL值的大小
- #include<stdio.h>
- #include<assert.h>
-
- int my_strcmp(const char* str1, const char* str2)
- {
- assert(str1 && str2);
- while (*str1 == *str2&&*str1!='\0')//如果*str1和*str2相等且不等于'\0'时进行循环往下查找不相等的位置
- {
- str1++;
- str2++;
- }
- return *str1 - *str2;//两个字符的ASCLL值相减比较大小
- }
- int main()
- {
- char arr1[] = "zhangsan";
- char arr2[] = "lisi";
- int ret = my_strcmp(arr1, arr2);
- printf("%d\n", ret);
- return 0;
- }
运行结果:
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr1[20] = "abcde";
- char arr2[20] = { 0 };
- strncpy(arr2, arr1, 4);
- printf("%s\n", arr2);
- return 0;
- }
运行结果:
模拟实现strncpy
- #include<stdio.h>
- #include<assert.h>
-
- char* my_strncpy(char* dest, const char* str, size_t count)//该count的单位为字节
- {
- assert(dest && str);
- char* ret = dest;
- while (count--)//循环的次数即为要赋值的个数
- {
- *dest++ = *str++;
- }
- return ret;
- }
- int main()
- {
- char arr1[20] = "abcde";
- char arr2[20] = { 0 };
- my_strncpy(arr2, arr1, 4);
- printf("%s\n", arr2);
- return 0;
- }
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr1[20] = "Hello ";
- char arr2[20] = "World and Earth";
- strncat(arr1, arr2, 5);
- printf("%s\n", arr1);
- return 0;
- }
运行结果:
模拟实现strncat
- #include<stdio.h>
- #include<assert.h>
- char* my_strncat(char* dest, const char* str, size_t count)
- {
- assert(dest && str);
- char* ret = dest;
- while (*dest)//找到'\0'的位置
- {
- dest++;
- }
- while (count--)//循环执行的次数
- {
- *dest++ = *str++;
- }
- return ret;
- }
- int main()
- {
- char arr1[20] = "Hello ";
- char arr2[20] = "World and Earth";
- my_strncat(arr1, arr2, 5);
- printf("%s\n", arr1);
- return 0;
- }
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr1[] = "zhangsan";
- char arr2[] = "zhangyi";
- int ret1 = strncmp(arr1, arr2, 5);
- int ret2 = strncmp(arr1, arr2, 6);
- printf("%d\n", ret1);
- printf("%d\n", ret2);
- return 0;
- }
运行结果:
模拟实现strncmp
- #include<stdio.h>
- #include<assert.h>
-
- int my_strncmp(const char* str1, const char* str2, size_t count)
- {
- assert(str1 && str2);
- while (count&&*str1==*str2)//如果*str1和*str2相等且不等于'\0'时进行循环往下查找不相等的位置
- {
- count--;
- str1++;
- str2++;
- }
- if (count == 0)//如果循环的次数全部执行说明要比较的字符串相等
- return 0;
- return *str1 - *str2;//ASCLL值相减比较大小
- }
- int main()
- {
- char arr1[] = "zhangsan";
- char arr2[] = "zhangyi";
- int ret1 = my_strncmp(arr1, arr2, 5);
- int ret2 = my_strncmp(arr1, arr2, 6);
- printf("%d\n", ret1);
- printf("%d\n", ret2);
- return 0;
- }
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr1[] = "abcdefabcd";
- char arr2[] = "efa";
- char* ret = strstr(arr1, arr2);
- printf("%s\n", ret);
- return 0;
- }
运行结果:
模拟实现strstr
该函数的第一个参数为母字符串的首元素地址,第二个参数为子字符串的首元素的地址
- #include<stdio.h>
- #include<assert.h>
-
- char* my_strstr(const char* str1, const char* str2)
- {
- assert(str1 && str2);
- char* ret = str1;//记录开始比较的地址
- while (*str1 == *str2 && *str2 != '\0')//寻找子字符串
- {
- str1++;
- str2++;
- }
- if (*str2 == '\0')//找到了
- return ret;
- ret++;
- if (*str1 == '\0')//找不到
- return NULL;
- }
- int main()
- {
- char arr1[] = "abcdefabcd";
- char arr2[] = "efa";
- char* ret = strstr(arr1, arr2);
- printf("%s\n", ret);
- return 0;
- }
strtok能将规定的字符转换为'\0'
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char arr1[] = "zxcvbnm@qq.com";
- const char* str = "@.";//能被转为'\0'
- char arr2[20] = { 0 };
- strcpy(arr2, arr1);
- char* ret = NULL;
- for (ret = strtok(arr2, str); ret != NULL; ret = strtok(NULL, str))//for循环中第一个表达式找到arr1首元素的地址,该字符串到了'\0'才会停下,第3个表达式的NULL能找到上一次记录的地址
- {
- printf("%s\n", ret);
- }
- return 0;
- }
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- int* p = (int*)malloc(_CRT_INT_MAX);//用malloc动态申请_CRT_INT_MAX个字节的内存,_CRT_INT_MAX值太大内存不足,产生错误
- if (p == NULL)//如果为空指针的话会将错误码返回到errno中
- {
- printf("%s\n", strerror(errno));//用strerror接受错误的信息并打印出来
- }
- return 0;
- }
运行结果:
内容到这里结束了,如果感觉有帮助的话点个赞呗
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。