当前位置:网站首页>用C语言实现重写strcmp等四个函数
用C语言实现重写strcmp等四个函数
2022-04-23 05:46:00 【OceanKeeper1215】
#include <stdio.h>
#include <string.h>
#include <assert.h>
size_t str_len(const char* str)
{
// if(NULL == str) return -1;
assert(NULL != str);
const char* tmp = str;
while(*tmp) tmp++;
return tmp - str;
}
char* str_cpy(char* dest,const char* src)
{
assert(NULL != dest && NULL != src);
char* tmp = dest;
while(*tmp++ = *src++);
return dest;
}
char* str_cat(char* dest,const char* src)
{
assert(NULL != dest && NULL != src);
char* tmp = dest;
while(*tmp) tmp++;
while(*tmp++ = *src++);
return dest;
}
int str_cmp(const char* s1,const char* s2)
{
assert(NULL != s1 && NULL != s2);
while(*s1 && *s1 == *s2) s1++,s2++;
// return *s1-*s2;
if(*s1 > *s2)
return 1;
else if(*s1 < *s2)
return -1;
else
return 0;
}
int main(int argc,const char* argv[])
{
char str[256] = "hehe";
char* str1 = NULL;
// strcpy(str,str1);
printf("str_len: %d\n",str_len(str1));
printf("str_cpy: %s\n",str_cpy(str,"abc"));
printf("str_cat: %s\n",str_cat(str,"qqq"));
printf("str_cmp: %d\n",str_cmp("abb","abb"));
}
版权声明
本文为[OceanKeeper1215]所创,转载请带上原文链接,感谢
https://blog.csdn.net/OceanKeeper1215/article/details/121249560
边栏推荐
- P1586 solution to tetragonal theorem
- Rainbow (DP)
- Explanation of login page
- Generate excel template (drop-down selection, multi-level linkage)
- [leetcode 290] word rules
- Busybox initrd and initialization process
- Customized communication between threads (reentrantlock)
- Framework analysis 1 Introduction to system architecture
- [leetcode 383] ransom letter
- 20 excellent plug-ins recommended by idea
猜你喜欢
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
[leetcode 59] spiral matrix II
从源代码到可执行文件的过程
基于pygame库编写的五子棋游戏
1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition
[leetcode 202] happy number
Why does the subscript of the array start from 0 instead of 1?
几行代码教你爬取LOL皮肤图片
檢測技術與原理
[leetcode 67] sum of two binary numbers
随机推荐
D. Optimal partition segment tree optimization DP
根据SQL语句查询出的结果集,将其封装为json
小区房价可视化
Sakura substring thinking
定位器
MySQL occasional Caton
[leetcode 290] word rules
进程间通信的方式
Addition, deletion, query and modification of data
檢測技術與原理
Framework analysis 2 Source code - login authentication
Stability building best practices
Database - sorting data
[leetcode 954] double pair array
批量导出Arcgis属性表
Import of data
selenium+webdriver+chrome实现百度以图搜图
Rust 的多线程安全引用 Arc
卡尔曼滤波与惯性组合导航
Type conversion in C #