当前位置:网站首页>字符函数和内存函数
字符函数和内存函数
2022-07-21 04:59:00 【今天也要写bug】
字符串函数和内存函数
一、strlen
int my_strlen1(const char* s)
{
int cnt = 0;
while (*s)
{
s++;
cnt++;
}
return cnt;
}
int my_strlen2(const char* s)
{
if (!*s) return 0;
return my_strlen2(s + 1) + 1;
}
int my_strlen3(const char* s)
{
const char* p = s;
while (*p) p++;
return (int)(p - s);
二、strcpy
char* my_strcpy(char* dest, const char* src)
{
char* ret = dest;
assert(*src);
while (*dest++ = *src++);
return ret;
}
三、strcat
char* my_strcat(char* dest, const char* src)
{
assert(*dest);
assert(*src);
char* ret = dest;
while (*dest)
dest++;
while (*dest++ = *src++);
return ret;
}
四、strstr
char* my_strstr(const char* str1, const char* str2)
{
if (!*str2) return (char*)str1;
char* cp = (char*)str2;
char* s1, *s2;
while (*cp)
{
s1 = (char*)cp;
s2 = (char*)str2;
if (*str2) return (char*)str1;
while (*s1 & *s2 && !(*s1 - *s2))
s1++, s2++;
if (!*s2) return cp;
cp++;
}
return nullptr;
}
五、strcmp
int my_strcmp(const char* src, const char* dst)
{
assert(*src);
assert(*dst);
int ret = 0;
while (!(ret = (unsigned char*)src - (unsigned char*)dst) && *dst)
dst++, src++;
if (ret < 0) return -1;
else if (ret) return 1;
return 0;
}
六、memcpy
void* my_memcpy(void* dst, const void* src, size_t count)
{
assert(dst);
assert(src);
void* ret = dst;
while (count--)
{
*(char*)dst = *(char*)src;
dst = (char*) dst + 1;
src = (char*)src + 1;
}
return ret;
}
七、memmove
void* my_memmove(void* dst, const void* src, size_t count)
{
void* ret = dst;
if (dst <= src || (char*)dst >= ((char*)src + count)) {
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
}
else {
dst = (char*)dst + count - 1;
src = (char*)src + count - 1;
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst - 1;
src = (char*)src - 1;
}
}
return ret;
}
边栏推荐
- Summary of arithmetic operations in shell
- Job regular sed
- 序列模型(一)- 循环序列模型
- Metauniverse iwemeta: the new cover of time magazine, metauniverse will change everything
- 【To .NET】.NET Core Web API开发流程知识点整理[入门]
- Why write unit tests? How to write unit tests?
- Generate multipartfile in the project and call other services
- ES6的数组、对象拷贝
- 深度解读财团参与Twitter私有化的投资逻辑
- input: dynamic input is missing dimensions in profile
猜你喜欢
The ability to detect movement in vivo and build a safe and reliable payment level "face brushing" experience
Machine learning sklearn dataset
我的UI自动化测试的感悟
元宇宙iwemeta:风口上的脑机接口,偷偷的解密大脑
426. 将二叉搜索树转化为排序的双向链表
Mes réflexions sur les tests automatisés d'assurance - chômage
NR modulation 4-AM
Sequence model (I) - cyclic sequence model
深度解读财团参与Twitter私有化的投资逻辑
接口测试常用工具及测试方法
随机推荐
【To .NET】.NET Core Web API开发流程知识点整理[进阶]
2022DASCTF MAY web
ECCV 2022 open source | target segmentation for 10000 frames of video
PHP如何实现在用户注册时查找数据库中是否有相同用户名
input: kMAX dimensions in profile 0 are [2,3,128,128] but input has static dimensions [1,3,128,128]
STL list输出和增加
【To .NET】.NET Core Web API开发流程知识点整理[入门]
cuMemcpyHtoDAsync failed: invalid argument
从平面设计转行软件测试,喜提11K+13薪,回头看看我很幸运
ENS阅读笔记
让你告别996的项目维护方式
Sentinel vs Hystrix 限流对比,到底怎么选?
odoo神操作后台调用路由接口
jimu积木报表打印时多一页空白页-问题解决
Content with element type "resultmap" must match "(constructor?, id*, result*, association*, collection*, discriminato?) “
微信小程序支付
C——编译预处理
Project Management Maturity Model and project quantitative management
Learning notes introduction to explain
STM32——定位模块ATGM336H,数据解析,提取经纬度