当前位置:网站首页>Implement common C language string processing functions
Implement common C language string processing functions
2022-06-23 10:32:00 【stone_ three hundred and twenty-two】
1.strcpy()
char *my_strcpy(char *s1, const char *s2)
{
char* p1 = s1;
const char* p2 = s2;
while (*p1++ = *p2++)
{
}
return s1;
}
2.strlen()
unsigned int my_strlen(const char* s)
{
const char* p = s;
while (*p++)
{
}
return p - s - 1;
}
3.strcat()
char* my_strcat(char* s1, const char* s2)
{
char* p1 = s1;
const char* p2 = s2;
unsigned int len = strlen(s1);
p1 += len;
while (*p1++ = *p2++)
{
}
return s1;
}
4.strcmp()
int my_strcmp(const char* s1, const char* s2)
{
const char* p1 = s1;
const char* p2 = s2;
while (*p1 || *p2)
{
if (*p1 - *p2)
{
return *p1 - *p2 > 0 ? 1 : -1;
}
p1++;
p2++;
}
return 0;
}
5.atoi()
int my_atoi(const char* nptr)
{
const char* p = nptr;
int sign = 1;
int num = 0;
while (*p == ' ')
{
p++;
}
sign = (*p == '-') ? -1 : 1;
if (*p == '+' || *p == '-')
{
p++;
}
while (*p >= '0' && *p <= '9')
{
num = num * 10 + *p - '0';
p++;
}
return num*sign;
}
6.itoa()
char* my_itoa(int value, char* str, int base)
{
char* p = str;
int reverse_start = 0;
if (value < 0)
{
reverse_start = 1;
*p++ = '-';
value = -value;
}
else if (value == 0)
{
*p++ = '0';
}
while (value)
{
*p++ = value % base + '0';
value /= base;
}
*p = '\0';
int i, j;
for (i = reverse_start, j = strlen(str) - 1; i < j; i++, j--)
{
char tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
return str;
}
7.memmove()
void* my_memmove(void* dest, void* src, size_t num)
{
char* psrc = (char*)src;
char* pdest = (char*)dest;
if (pdest <= psrc || pdest > psrc + num)
{
for (size_t i = 0; i < num; i++)
{
*pdest++ = *psrc++;
}
}
else
{
pdest += num;
psrc += num;
for (size_t i = 0; i < num; i++)
{
*(--pdest) = *(--psrc);
}
}
return dest;
}
边栏推荐
- 【软件与系统安全】堆溢出
- JVM简单入门-01
- NOI OJ 1.3 16:计算线段长度 C语言
- Personal blog system graduation project opening report
- How to write a literature review? What should I do if I don't have a clue?
- Unity technical manual - lifecycle lifetimebyemitterspeed - color in the cycle coloroverlifetime- speed color colorbyspeed
- Is IPv6 faster than IPv4?
- Confessing with Snake games (with source code)
- Cool photo album code, happy birthday to the object!
- Pycharm installation tutorial, super detailed
猜你喜欢
随机推荐
NOI OJ 1.2 06:浮点数向零舍入
2021-05-11static关键字
Golang 快速上手 (3)
Mysql-03. Experience of SQL optimization in work
TTY驱动框架
数值计算方法
2021-05-11 abstract class
Pycharm installation tutorial, super detailed
社招腾讯高P(高级产品经理)的面试手册
2021-05-12接口的定义与实现
Google Earth Engine(GEE)——用不同方法计算slope对比案例分析
用贪吃蛇小游戏表白(附源码)
JVM easy start-01
[software and system security] heap overflow
圖片存儲--引用
Noi OJ 1.3 13: reverse output of a three digit C language
Confessing with Snake games (with source code)
Experience of using thread pool in project
OpenCloudOS使用snap安装.NET 6
Personal blog system graduation project opening report









