当前位置:网站首页>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;
}
边栏推荐
- SQL教程之 5 个必须知道的用于操作日期的 SQL 函数
- NOI OJ 1.3 05:计算分数的浮点数值 C语言
- Noi OJ 1.3 04: C language with remainder Division
- 2021-05-07 package inheritance super this
- 线程池在项目中使用的心得体会
- What is a good quick development framework like?
- Spring recruitment interview experience summary (technical post)
- Description of directory files of TLBB series of Tianlong Babu - netbill server [ultra detailed]
- 2021-05-11static关键字
- Noi OJ 1.3 15: apple and bug C language
猜你喜欢

What does NFTs, Web3 and metauniverse mean for digital marketing?

Bugs encountered in Oracle

开源二进制文件静态漏洞分析工具BinAbsInspector安装使用

Different methods of PivotTable in SQL tutorial

Pycharm installation tutorial, super detailed

Analysis of LinkedList source code

R and rstudio download and install detailed steps

Experience of using thread pool in project

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

Nuxt. Differences between JS spa and SSR
随机推荐
NOI OJ 1.4 03:奇偶数判断 C语言
MySQL Basics - Notes
春招面试经验汇总(技术岗)
构建信创产业生态,移动云立足全栈自主创新连放大招
Opencloudos uses snap to install NET 6
当 Pandas 遇见 SQL,一个强大的工具库诞生了
2021-05-11 abstract class
解决audio自动播放无效问题
Cool photo album code, happy birthday to the object!
SQL教程之 5 个必须知道的用于操作日期的 SQL 函数
C语言结构体字节对齐问题
Nuxt. Differences between JS spa and SSR
How to solve the problem that easycvr does not display the interface when RTMP streaming is used?
2021-04-16 method overload parameter transfer
实战监听Eureka client的缓存更新
Mysql-03. Experience of SQL optimization in work
R和RStudio下载安装详细步骤
Noi OJ 1.4 03: odd even judging C language
NOI OJ 1.2 整数数据类型存储空间大小
华为云·维享会交流平台简介