当前位置:网站首页>实现常用C语言字符串处理函数
实现常用C语言字符串处理函数
2022-06-23 10:20:00 【stone_322】
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;
}
边栏推荐
- 文献综述怎么写 ,一直没头绪写不出来怎么办?
- How to solve the problem that easycvr does not display the interface when RTMP streaming is used?
- 解决audio自动播放无效问题
- 数值计算方法
- 2021-04-27类和对象
- R和RStudio下载安装详细步骤
- Tencent tangdaosheng: practice "science and technology for the good" and promote sustainable social value innovation
- [day 23] given an array of length N, insert element x into the position specified by the array | array insertion operation 4
- SQL writing problem to calculate the ring ratio of the current month and the previous month
- 2021-05-07 package inheritance super this
猜你喜欢

SQL writing problem to calculate the ring ratio of the current month and the previous month

文件IO(1)

Solve the problem that Preview PDF cannot be downloaded

Picture storage -- Reference

JVM easy start-02

技术创造价值,手把手教你薅羊毛篇

Different methods of PivotTable in SQL tutorial

Unity技术手册 - 形状(Shape)子模块 - Sprite、SpriteRenderer及生命周期内速度(Velocity over Lifetime)

搭建一个QQ机器人叫女友起床

2021-05-07 constructor
随机推荐
OpenCloudOS使用snap安装.NET 6
5次登陆失败,限制登录实践
Cool photo album code, happy birthday to the object!
Nuxt. Differences between JS spa and SSR
Nuxt.js spa与ssr的区别
R和RStudio下载安装详细步骤
Set up a QQ robot for ordering songs, and watch beautiful women
NOI OJ 1.3 11:计算浮点数相除的余数 C语言
当 Pandas 遇见 SQL,一个强大的工具库诞生了
太无奈!微软停售 AI 情绪识别等技术,直言:“法律跟不上 AI 的发展”
Build the information and innovation industry ecology, and make mobile cloud based on the whole stack of independent innovation
NOI OJ 1.3 20:计算2的幂 C语言
web技术分享| 【高德地图】实现自定义的轨迹回放
2021-04-16递归
2021-04-16方法重载 传参
数值计算方法
STM32F1与STM32CubeIDE编程实例-红外寻迹传感器驱动
2021-04-16 recursion
2021-05-07 package inheritance super this
NOI OJ 1.3 16:计算线段长度 C语言