字符串函数的模拟实现
来源:互联网 发布:手柄 知乎 编辑:程序博客网 时间:2024/06/02 04:31
#include <stdio.h>#include <stdlib.h>#include <string.h>char *my_strchr( char *dest, const char src){while (*dest++){if (*dest == src)return dest;}return NULL;}int main(){char arr[20] = "abcddddef";char a = 'd'; char *ret = 0;ret = my_strchr(arr, a);printf("%s\n",ret);system("pause");return 0;}
#include <stdio.h>#include <stdlib.h>#include <string.h>char *my_strrchr(char *dest,const char src){char *temp = NULL;while (*dest++){if (*dest == src)temp = dest;}return temp;}int main(){char arr[20] = "asddddddf";char a = 'd';char *ret = 0;ret = my_strrchr(arr, a);printf("%s\n",ret);system("pause");return 0;}
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>int my_strncmp(const char *s1,const char *s2,size_t n){assert(s1 != NULL); assert(s2 != NULL);while (n--){if ((*s1 == *s2)&&*s1){s1++;s2++;}else if (*s1 != *s2){return (*(unsigned char*)s1 - *(unsigned char*)s2);}}return 0;}int main(){char *s1 = "wangmazi";char *s2 = "wisi";printf("%d\n", my_strncmp(s1, s2, 2));system("pause");return 0;}
#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <string.h>int my_strcmp(const char* s1, const char* s2){assert(s1);assert(s2);while ((*s1 == *s2)&&*s1){++s1;++s2;}return (*(unsigned char*)s1 - *(unsigned char*)s2);}int main(){char *p = "abcdef";char arr[] = "abcdef";if (my_strcmp(p, arr) < 0){printf("%s\n", p);}system("pause");return 0;}
#include <stdio.h>#include <stdlib.h>#include <assert.h>char* my_strcat(char* dest,const char* src){assert(dest != NULL);assert(src != NULL);char* ret = dest;while (*dest){++dest;}while (*dest++ = *src++){;}return ret;}int main(){char arr[20] = "week";char *p = "end";printf("%s\n", my_strcat(arr, p));system("pause");return 0;}#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>char *my_strncat(char *dest,const char *src,size_t n){assert(dest!=NULL);assert(src!=NULL);char *s1 = dest;while (*dest){++dest;}while (n--){*dest++ = *src++;}return s1;}int main(){char arr[20] = "week";char *p = "end";my_strncat(arr, p, 2);printf("%s\n",arr);system("pause");return 0;}#include <stdio.h>#include <stdlib.h>#include <assert.h>char* my_strcpy(char *dest, const char* src){assert(dest);assert(src);char* ret = dest;while (*dest++ = *src++){;}return ret;}int main(){char arr[20] = { 0 };char* p = "hello world";printf("%s\n", my_strcpy(arr, p));system("pause");return 0;}
#include <stdio.h>#include <stdlib.h>#include <assert.h>char *my_strncpy(char *dest,const char*src,size_t n){assert(dest!=NULL);assert(src!=NULL);char *s1 = dest;while ((n) && *src){*dest++ = *src++;n--;}if (n){while (--n){*dest++ = '\0';}}return s1;}int main(){char arr[20] = { 0 };char *p = "asddd";my_strncpy(arr, p, 3);printf("%s\n",arr);system("pause");return 0;}
#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <string.h>int my_strlen(const char* src){assert(src);int count = 0;while (*src++){count++;}return count;}int main(){ printf("%d\n", my_strlen("abcdef"));system("pause");return 0;}#include <stdio.h>#include <stdlib.h>int my_strlen(const char* src){if (*src){return (1 + my_strlen(src + 1));}else{return 0;}}int main(){printf("%d\n", my_strlen("abcdef"));system("pause");return 0;}
#include <stdlib.h>#include <string.h>#include <assert.h>char* my_strstr(char* dest, const char* src){assert(dest != NULL);char* cur = dest;const char* ptr = src;while (*dest++){cur = dest;while (*cur == *ptr){++cur;++ptr;}if (*ptr == '\0'){return dest;}else{ptr = src;}}return NULL;}int main(){char* p = "abbbbcdef";char arr[] = "g";printf("%s\n", my_strstr(p, arr));system("pause");return 0;}
阅读全文
0 0
- 字符串函数的模拟实现
- 字符串函数模拟实现
- 字符串函数模拟实现
- 字符串函数模拟实现
- 一些字符串操作函数的模拟实现
- 模拟实现常用字符串函数
- 模拟实现字符串操作函数
- 字符串操作函数模拟实现
- 模拟实现部分字符串函数
- 模拟实现字符串操作函数
- 字符串逻辑比较函数---StrCmpLogicalW的模拟实现
- 字符串逻辑比较函数---StrCmpLogicalW的模拟实现
- 字符串函数之strlen() , strcpy() 的模拟实现
- 常见字符串函数的模拟实现(一)(c语言)
- 模拟实现strncat函数(字符串追加函数)
- 字符串操作函数的模拟
- 模拟实现C语言中的字符串函数
- 字符串操作函数模拟实现大全
- Spring--hibernate整合_事务管理
- linux——CentOS真机安装过程中遇到的问题
- 直方图均衡(Opencv,十行代码)
- django1.11.5版本中后台字段类型提示错误解决方法,个人资料分表方法用一对一关联
- Redis的应用场景
- 字符串函数的模拟实现
- Nginx反向代理的配置
- redis存对象
- 插入排序
- JSP(1)入门
- 用 Keras 编写你的第一个人工神经网络(Python)—— Jinkey 翻译
- 关于SSM @ResponseBody 返回数据中文乱码解决问题
- HDU6205-card card card
- 动态规划