C语言模拟实现strncpy、strncpy、strncat、strstr和strrstr函数实现
来源:互联网 发布:java约瑟夫环问题 编辑:程序博客网 时间:2024/05/18 21:42
以下是我用C语言模拟实现的部分字符串函数:
1、strncpy函数的实现
#include<stdio.h>#include<assert.h>#include<iostream>char*my_strncpy(char*dest, const char*src, int n){assert(dest != NULL);assert(src != NULL);int i = 0;char *tmp = dest;while ((i++<n)&&(*tmp++ = *src++ )!= '\0'){;}return dest;}int main(){char str1[] = "1234567890";char str2[] = "abcde";my_strncpy( str1,str2, 6);printf("%s\n", str1);system("pause");return 0;}
2、strncpy函数的实现
#include<stdio.h>#include<iostream>#include<assert.h>int my_strncmp(const char*str1, const char*str2,int n){assert(str1 != NULL);assert(str2 != NULL);while ((*str1!='\0')&&(*str2!='\0')&&n){ if ((*str1 - *str2) > 0)return 1; if ((*str1-*str2)<0)return -1;str1++;str2++;n--;}if (*str1 == '\0'&&*str2 != '\0')return -1; if (*str1 != '\0'&&*str2 == '\0')return 1;return 0;}int main(){char str1[] = "abcdef";char str2[] = "abcmln";int ret = my_strncmp(str1, str2,4);printf("%d\n", ret);system("pause");return 0;}
3、strncat函数实现
#include<stdio.h>#include<assert.h>#include<iostream>char*my_strncat(const char*src,char*dest, int n){assert(src != NULL&&dest != NULL);char*tmp = dest;while (*tmp != '\0'){tmp++;}while ((n--) && (*tmp++ = *src++) != '\0'){;}return dest;}int main(){char str1[20] = "hello ";char str2[6] = "word!";my_strncat(str2, str1, 2);printf("%s\n", str1);system("pause");return 0;}
4、strstr函数的实现
#include<stdio.h>#include<assert.h>#include<string.h>#include<iostream>char*my_strstr(char*str1, char*str2){assert(str1 != NULL&&str2 != NULL);int i = 0;int j = 0;char*ret = 0;for (i = 0; str1[i] != 0; i++){for (j = 0; str2[j] != 0; j++){if (str1[i + j] != str2[j]){break;}}if (str2[j] == 0)return str1 + i;}return NULL;}int main(){char str[] = "abbcdbcdefmln";char str1[] = "bcd";//char *tmp=my_strstr(str, str1);printf("%s", my_strstr(str, str1));system("pause");return 0;}
5、strrstr函数的实现
#include<stdio.h>#include<assert.h>#include<iostream>char*my_strrstr(char*str1, char*str2){assert(str1 != NULL&&str2 != NULL);int i = 0;int j = 0;char*ret = 0;for (i = 0; str1[i] != 0; i++){for (j = 0; str2[j] != 0; j++){if (str1[i + j] != str2[j]){break;}}if (str2[j] == 0)ret=str1+i;}return ret;}int main(){char str[] = "abbcdbcdefmln";char str1[] = "bcd";//char *tmp=my_strstr(str, str1);printf("%s", my_strrstr(str, str1));system("pause");return 0;}
阅读全文
2 0
- C语言模拟实现strncpy、strncpy、strncat、strstr和strrstr函数实现
- c语言中部分限制字符函数的实现strncmp,strncat,strncpy,strstr,strrstr
- 模拟实现C语言strncpy.strncat.strncmp函数
- 【C语言】模拟实现strncpy、strncat、strncmp函数
- 【C语言】模拟实现strncpy strncat strncmp strchr strrchr函数
- C语言~模拟实现strncpy、strncat、strncmp函数
- [C语言]模拟实现strcpy/strncpy/strcat/strncat/strcmp/strncmp/strstr
- 模拟实现C库函数strncpy、strncat、strncmp
- 模拟实现C库函数:strncpy、strncat和strncmp
- 【C语言】 实现strncpy,strncat,strncmp
- 模拟实现strncpy,strncat,strncmp
- 模拟实现strncpy,strncat,strncmp
- 模拟实现strncpy,strncat,strncmp
- 模拟实现strncpy,strncat,strncmp
- 【C语言】模拟实现strcpy()、strncpy()函数
- C语言模拟实现strcpy、strncpy、strcmp、strcat、strstr
- 模拟实现strcmp,strncmp,strstr,strcat,strncat,strcpy,strncpy,strlen ,memcpy,memmove等函数
- 字符串训练(二)模拟实现strstr strncpm strncat strncpy函数
- centos开启ftp服务
- JavaScript的基础知识
- 在全景场景里添加平面视频
- 高中生就可学会编程--写在前面的话
- 算法笔记:二进制数有多少个1
- C语言模拟实现strncpy、strncpy、strncat、strstr和strrstr函数实现
- codeforces 551A GukiZ and Contest
- MultiByteToWideChar的用法
- 一些不错的网站
- ReactNative在安卓上遇到的问题
- ubuntu包管理命令apt
- Team 1——Add More Zero
- 随笔记:Mysql 对于换行符回车符 查询失效的处理
- 安装fastDFS遇到的问题总结