当前位置:网站首页>C语言模拟实现所有字符函数
C语言模拟实现所有字符函数
2022-06-29 12:27:00 【欧橘猫】
string.h 字符函数模拟
模拟 strlen 计算字符串个数
strlen 函数的参数
size_t strlen ( const char * str );
- strlen 是计算字符个数
- strlen 是计算 ‘\0’ 之前的字符的长度
代码实现:
方法一
以计数的方式计算
int my_strlen(char* str)
{
int count = 0;
while (*str)
{
str++;
count++;
}
return count;
}
int main()
{
char arr[] = {
"abcdef" };
int ret = my_strlen(arr);
printf("%d\n", ret);
return 0;
}
方法二
不能创建临时变量
以函数递归的方式计算
int my_strlen(char* str)
{
if (*str == 0)
return 0;
else
return 1 + my_strlen(str+1);
}
int main()
{
char arr[] = {
"abcdef" };
int ret = my_strlen(arr);
printf("%d\n", ret);
return 0;
}
方法三
以指针 - 指针的方式计算
int my_strlen(char* str)
{
char* q = str;
while (*q)
{
q++;
}
return q - str;
}
int main()
{
char arr[] = {
"abcdef" };
int ret = my_strlen(arr);
printf("%d\n", ret);
return 0;
}
模拟 strcpy 字符串拷贝
strcpy 函数的参数
char* strcpy(char * destination, const char * source );
- strcpy 是字符串拷贝函数
- strcpy 里的第一个参数是拷贝的
目标空间,第二个参数是拷贝的内容 - 注意
1.拷贝的目标空间必须足够大,拷贝后能够存放字符串
2 参数的顺序
3
代码实现:
#include<assert.h>
char* my_strcpy(char* arr, const char* str)
{
char* s = str;
assert(arr);
assert(str);
while (*arr++ = *str++)
{
;
}
return s;
}
int main()
{
char arr[] = {
"#####################" };
char str[] = {
"hello china!" };
printf("%s\n", my_strcpy(arr, str));
return 0;
}
模拟 strcat 字符串追加
函数的参数
char * strcat ( char * destination, const char * source );
- strcat 函数会在字符串最后位 ‘\0’ 的位置添加字符串
代码实现:
char* my_strcat(char* arr, char* str)
{
char* s = arr;
assert(arr && str);
while (*arr)
{
arr++;
}
while (*arr++ = *str++)
{
;
}
return s;
}
int main()
{
char arr[15] = {
"hello " };
char str[] = {
"china!" };
my_strcat(arr, str);
printf("%s\n", arr);
}
模拟 strcmp 字符串比较
strcmp 函数的参数
int strcmp ( const char * str1, const char * str2 );
- strcmp 是一个一个字符比较
相等返回0
第一个字符串大于第二个字符串,返回大于0的数字
第一个字符串小于第二个字符串,则返回小于0的数字
代码实现:
int my_strcmp(const char* arr, const char* str)
{
assert(arr && str);
while (*arr == *str && *str)
{
if (*arr == '\0')
{
return 0;
}
arr++;
str++;
}
return *arr - *str;
}
int main()
{
char arr[] = {
"abcde" };
char str[] = {
"abd" };
int ret = my_strcmp(arr, str);
if (ret > 0)
printf("arr > str");
else if (ret < 0)
printf("arr < str");
else
printf("arr == str");
}
模拟 strncpy 长度限制字符串拷贝
strncpy 函数的参数
char * strncpy ( char * destination, const char * source, size_t num );
- strncpy 可能只会拷贝字符串的某一段,也就有可能不拷贝 ‘\0’ ,所以在实现的时候必须要注意
代码实现:
char* my_strncpy(char* arr, const char* str, size_t num)
{
assert(arr && str);
char* s = str;
while (num--)
{
*arr++ = *str++;
}
*arr++ = '\0';
return s;
}
int main()
{
char arr[] = {
"#####################" };
char str[] = {
"hello china!####" };
my_strncpy(arr, str, 5);
printf("%s\n",arr);
return 0;
}
模拟 strncat 字符串追加
strncat 函数参数
char * strncat ( char * destination, const char * source, size_t num );
代码实现:
char* my_strncat(char* arr, char* str, size_t num)
{
assert(arr && str);
char* s = arr;
while (*arr++)
{
;
}
//寻找到 *arr 为 '\0' 又进行了一次 ++
//所以这里需要 -- 找到 '\0' 真正位置
arr--;
while (num--)
{
*arr++ = *str++;
}
*arr++ = '\0';
return s;
}
int main()
{
char arr[20] = {
"hello " };
char str[] = {
"china! For a dettet tomorrow!" };
my_strncat(arr, str, 6);
printf("%s\n", arr);
return 0;
}
模拟 strncmp 字符串比较
strncmp 函数参数
int strncmp ( const char * str1, const char * str2, size_t num );
- 实现的方式和strcmp 都是一样的,只是加多了一个参数
代码实现:
int my_strncmp(char* arr, char* str, size_t num)
{
while (num-- && (*arr - *str == 0) && *str)
{
arr++;
str++;
}
if (*arr > *str)
return 1;
else if (*arr < *str)
return -1;
else
return 0;
}
int main()
{
char arr[10] = {
"abcde" };
char str[10] = {
"abyaa" };
int ret = my_strncmp(arr, str, 3);
if (ret > 0)
printf("arr > str");
else if (ret < 0)
printf("arr < str");
else
printf("arr == str");
return 0;
}
模拟 strstr
strstr 函数参数
char * strstr ( const char *str1, const char * str2);
- 作用:在A 字符串中查找是否包含 B字符串
如果 A 中包含 B ,则返回 B 在 A 中首次出现的的地址。
否则返回空指针! - 如果找到,返回的是字符串的地址,所以要用指针变量来接收
代码实现:
char* my_strstr(const char* arr, const char* str)
{
char* xp = arr;
char* s1 = 0;
char* s2 = 0;
while (*xp)//如果 *xp 为0了,证明 arr 中 不包含 str
{
s1 = xp;
s2 = str;
while (*s1 && *s2 && ( *s1 - *s2 == 0))//只有 *s1 *s2 且 *s1 *s2 相等时才进行查找
{
s1++;
s2++;
}
if (*s2 == '\0')
return xp;
xp++;//如果 s2 没有找完,那么 xp 进入下一个位置继续查找
}
return NULL;
}
int main()
{
char arr[] = {
"abcdefg" };
char str[] = {
"cdb" };
printf("%s\n", my_strstr(arr, str));
}
边栏推荐
- RT thread memory management
- 从零搭建Pytorch模型教程(四)编写训练过程--参数解析
- Interview shock 61: tell me about MySQL transaction isolation level?
- Pygame 精准检测图像碰撞
- CVPR 2022 | unknown target detection module study: learning unknown targets in video
- 如何计算win/tai/loss in paired t-test
- cnpm报错‘cnpm‘不是内部或外部命令,也不是可运行的程序或批处理文件
- Cvpr2022 𞓜 loss problem in weakly supervised multi label classification
- Matlab简单入门
- SCHIEDERWERK电源维修SMPS12/50 PFC3800解析
猜你喜欢

Adjacency matrix and adjacency table structure of C # realization graph

Precautions for Beifu controller connecting Panasonic EtherCAT servo
![Equidistant segmentation of surface rivers in ArcGIS [gradient coloring, pollutant diffusion]](/img/05/18fb41f78b9b57175d50dfece65535.png)
Equidistant segmentation of surface rivers in ArcGIS [gradient coloring, pollutant diffusion]

STK_GLTF模型

Interview shock 61: tell me about MySQL transaction isolation level?

Cvpr2022 | reexamine pooling: your receptive field is not the best

强大、优秀的文件管理软件评测:图片管理、书籍管理、文献管理

Comment calculer Win / Tai / Loss in paired t - test

cnpm报错‘cnpm‘不是内部或外部命令,也不是可运行的程序或批处理文件

Tutorial on building pytoch model from zero (IV) compiling training process -- Parameter Analysis
随机推荐
别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!
Evaluation of powerful and excellent document management software: image management, book management and document management
Don't build the wheel again. It is recommended to use Google guava open source tool class library. It is really powerful!
Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
C # realize the hierarchical traversal of binary tree
LeetCode_双指针_中等_328.奇偶链表
Deep understanding of volatile keyword
Definition of C # clue binary tree
UI file introduction in QT
Recommended model recurrence (I): familiar with torch rechub framework and use
树状数组应用(AcWing 242,243,244)
C#实现二叉树非递归中序遍历程序
C#通過中序遍曆對二叉樹進行線索化
服务器上的RTC时间与世界时间不一致解决办法
360数科新能源专项产品规模突破60亿
商品搜索引擎—推荐系统设计
Hutool tool class learning (continuous update)
AcWing第57场周赛
MATLAB求极限
Equidistant segmentation of surface rivers in ArcGIS [gradient coloring, pollutant diffusion]