当前位置:网站首页>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
边栏推荐
- Go file operation
- An example of linear regression based on tensorflow
- Format problems encountered in word typesetting
- k8s之实现redis一主多从动态扩缩容
- Operation of 2022 mobile crane driver national question bank simulation examination platform
- Submit local warehouse and synchronize code cloud warehouse
- 2022 tea artist (primary) examination simulated 100 questions and simulated examination
- Crawl the product data of cicada mother data platform
- ES6 face test questions (reference documents)
- C1 notes [task training chapter I]
猜你喜欢
![解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]](/img/5f/a80951777a0473fcaa685cd6a8e5dd.png)
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
![C1 notes [task training chapter I]](/img/2b/94a700da6858a96faf408d167e75bb.png)
C1 notes [task training chapter I]

Random number generation of C #

Using files to save data (C language)

2022 judgment questions and answers for operation of refrigeration and air conditioning equipment

Tell the truth of TS

2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展

解决允许在postman中写入注释请求接口方法

极致体验,揭晓抖音背后的音视频技术

Docker installation MySQL
随机推荐
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
C language array processing batch data
Selenium + phantom JS crack sliding verification 2
Cross domain settings of Chrome browser -- including new and old versions
QTableWidget使用讲解
Welcome to the markdown editor
极致体验,揭晓抖音背后的音视频技术
Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
String function in MySQL
MySQL_01_简单数据检索
2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
ES6 face test questions (reference documents)
JS forms the items with the same name in the array object into the same array according to the name
列表的使用-增删改查
Array rotation
Docker 安裝 Redis
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
Logic regression principle and code implementation