模拟实现C语言中的字符串函数
2016-04-30 11:50
531 查看
1、int strcmp(const char* dest, const char* str)
字符串比较函数,比较字符串dest和str,当dest>str时,返回值>0;当dest<str时,返回值<0;当dest=str时,返回值=0.
模拟实现代码:
2、int strlen(const char* dest)
字符串长度函数,求取字符串dest的长度(不包含\0)
模拟实现代码:
3、char* strcpy(char* dest, const char* str)
字符串拷贝函数,把从str地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间(str和dest所指的空间区域不能重叠且dest必须有足够的空间存储str的字符串)
模拟实现代码:
链式访问模拟实现代码:
4、cha
4000
r* strcat(char* dest, const char* str)
字符串追加函数,把str所指的字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'
#include<stdio.h>
#include<assert.h>
char* mystrcat(char* dest, const char* str)
{
assert(dest);
assert(str);
char *ret = dest;
while (*dest)
{
dest++;
}
while (*dest++ = *str++)
{
;
}
return ret;
}
int main()
{
char s1[20] = "abcde";
const char* s2 = "fghil";
printf("%s\n", mystrcat(s1, s2));
getchar();
return 0;
}
5、char* strstr(const char* dest, const char* str)
在字符串中查找子串,并返回dest字符串的其余部分,如果没有找到,返回NULL
模拟实现代码:
6、char* strrstr(char *dest,char* str)
在字符串中查找指定字符串最后一次出现的位置
#include<stdio.h>
#include<assert.h>
#include<string.h>
char* mystrrstr(const char* dest, const char* str)
{
assert(dest);
assert(str);
char *last = NULL;
char *cur = strstr(dest, str);
while (cur != NULL)
{
last = cur;
cur = strstr(cur + 1, str);
}
return last;
}
int main()
{
char* p = "abcdefgabcdefg";
char* q = "de";
char* ret = mystrrstr(p, q);
printf("%s\n", ret);
getchar();
return 0;
}
字符串比较函数,比较字符串dest和str,当dest>str时,返回值>0;当dest<str时,返回值<0;当dest=str时,返回值=0.
模拟实现代码:
#include<stdio.h> #include<assert.h> int mystrcmp(const char* dest, const char* str) { assert(dest); assert(str); while (*dest == *str) { if (*dest == '/0') { return 0; } dest++; str++; } return *dest - *str; } int main() { char* s1 = "dbcdef"; char* s2 = "dacd"; int ret = mystrcmp(s1, s2); if (ret > 0) { printf("s1>s2\n"); } else if (ret == 0) { printf("s1==s2\n"); } else { printf("s1<s2\n"); } getchar(); return 0; }
2、int strlen(const char* dest)
字符串长度函数,求取字符串dest的长度(不包含\0)
模拟实现代码:
#include<stdio.h> int mystrlen(const char* dest) { int count = 0; while (*dest!='\0') { count++; *dest++; } return count; } int main() { char* s1 = ""; int ret = mystrlen(s1); printf("字符串长度为%d", ret); getchar(); return 0; }
使用指针模拟实现代码:
#include<stdio.h> int mystrlen(const char* dest) { const char *str = dest; while (*str++) ; return str - dest - 1; } int main() { int len = mystrlen("abcdef"); printf("%d\n", len); getchar(); return 0; }
使用递归模拟实现代码:
#include<stdio.h> int mystrlen(const char* dest) { if (*dest == '\0') { return 0; } else { return 1+mystrlen(dest + 1); } } int main() { int ret = mystrlen("abcdef"); printf("字符串的长度为%d\n", ret); getchar(); return 0; }
3、char* strcpy(char* dest, const char* str)
字符串拷贝函数,把从str地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间(str和dest所指的空间区域不能重叠且dest必须有足够的空间存储str的字符串)
模拟实现代码:
#include<stdio.h> #include<assert.h> void mystrcpy(char* dest, const char* str) { assert(dest); assert(str); while (*dest++ = *str++) { ; } *dest = '\0'; } int main() { char s1[20]; char* s2 = "belo"; mystrcpy(s1, s2); printf("%s\n",s1); getchar(); return 0; }
链式访问模拟实现代码:
#include<stdio.h> #include<assert.h> char* mystrcpy(char* dest, const char* str) { assert(dest); assert(str); char* ret = dest; while (*dest++ = *str++) { ; } *dest = '\0'; return ret; } int main() { char s1[20]; char* s2 = "belo"; printf("%d\n", strlen(mystrcpy(s1, s2))); getchar(); return 0; }
4、cha
4000
r* strcat(char* dest, const char* str)
字符串追加函数,把str所指的字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'
#include<stdio.h>
#include<assert.h>
char* mystrcat(char* dest, const char* str)
{
assert(dest);
assert(str);
char *ret = dest;
while (*dest)
{
dest++;
}
while (*dest++ = *str++)
{
;
}
return ret;
}
int main()
{
char s1[20] = "abcde";
const char* s2 = "fghil";
printf("%s\n", mystrcat(s1, s2));
getchar();
return 0;
}
5、char* strstr(const char* dest, const char* str)
在字符串中查找子串,并返回dest字符串的其余部分,如果没有找到,返回NULL
模拟实现代码:
#include<stdio.h> char* mystrstr(const char* dest, const char* str) { char* ptr = dest; char* p1 = NULL; char* p2 = NULL; while (*dest) { p1 = ptr; p2 = str; while (*p1++ == *p2++) { p1++; p2++; if (*p2=='\0') { return ptr; } } ptr++; } return NULL; } int main() { const char* s1 = "abcdefgh"; const char* s2 = "def"; char* ch = mystrstr(s1, s2); if (ch != NULL) { printf("%s\n", ch); } else { printf("not exist\n"); } getchar(); return 0; }
6、char* strrstr(char *dest,char* str)
在字符串中查找指定字符串最后一次出现的位置
#include<stdio.h>
#include<assert.h>
#include<string.h>
char* mystrrstr(const char* dest, const char* str)
{
assert(dest);
assert(str);
char *last = NULL;
char *cur = strstr(dest, str);
while (cur != NULL)
{
last = cur;
cur = strstr(cur + 1, str);
}
return last;
}
int main()
{
char* p = "abcdefgabcdefg";
char* q = "de";
char* ret = mystrrstr(p, q);
printf("%s\n", ret);
getchar();
return 0;
}
相关文章推荐
- 图解Win7下安装Borland C++ 3.1失败记
- UE4:C++中使用材质
- CDHtmlDialog的基本使用(C++调用JS函数的实现)
- 成为C++高手之如何入门
- C语言多文件编译
- C++基础实践指导(下)
- C++基础实践指导(上)
- vector不是模板 || list不是模板解决办法
- c++ 如何用一个函数实现两个字符串的比较
- C++文件和流
- C语言中的atoi函数的实现
- [C++]详解异常处理(Exception Handling) 及标准库异常处理类
- c++ 智能指针用法详解
- C语言中结构体的直接赋值
- c++:template使用中的常见报错
- 一起talk C栗子吧(第一百三十八回:C语言实例--文件操作:基于文件描述符一)
- c/c++常用宏、预编译指令
- C++中string类用法总结
- C语言:关于程序运行时的内存管理
- HDU 2024 C语言合法标识符(水题)