当前位置:网站首页>Rewrite four functions such as StrCmp in C language
Rewrite four functions such as StrCmp in C language
2022-04-23 18:03: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://yzsam.com/2022/04/202204230545105048.html
边栏推荐
- journal
- cv_ Solution of mismatch between bridge and opencv
- Open source key component multi_ Button use, including test engineering
- xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
- Notes on common basic usage of eigen Library
- .104History
- .105Location
- Oil monkey website address
- Auto.js 自定义对话框
- Crawl lottery data
猜你喜欢

2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination

Visualization of residential house prices

JS get link? The following parameter name or value, according to the URL? Judge the parameters after

2022 tea artist (primary) examination simulated 100 questions and simulated examination

2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition

MySQL auto start settings start with systemctl start mysqld

Gobang game based on pyGame Library

Using files to save data (C language)
Logic regression principle and code implementation

idea中安装YapiUpload 插件将api接口上传到yapi文档上
随机推荐
JS get link? The following parameter name or value, according to the URL? Judge the parameters after
列錶的使用-增删改查
I / O multiplexing and its related details
.104History
Auto. JS custom dialog box
Installation du docker redis
JS high frequency interview questions
Climbing watermelon video URL
.104History
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
How to install jsonpath package
String function in MySQL
Visualization of residential house prices
Summary of common server error codes
cv_ Solution of mismatch between bridge and opencv
Stanford machine learning course summary
Calculation of fishing net road density
re正則錶達式
The method of changing a value in the array and a value in the object of wechat applet
C language input and output (printf and scanf functions, putchar and getchar functions)