当前位置:网站首页>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
边栏推荐
- Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
- C# 网络相关操作
- Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
- C language input and output (printf and scanf functions, putchar and getchar functions)
- _ FindText error
- Crawler for querying nicknames and avatars based on qqwebapi
- .104History
- Climbing watermelon video URL
- Crack sliding verification code
- MySQL_01_简单数据检索
猜你喜欢
C language loop structure program
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
GDAL + ogr learning
C#的随机数生成
MySQL_ 01_ Simple data retrieval
JS forms the items with the same name in the array object into the same array according to the name
Nat commun | current progress and open challenges of applied deep learning in Bioscience
Jenkspy package installation
Go language JSON package usage
Gaode map search, drag and drop query address
随机推荐
Fashion classification case based on keras
Crawling mobile game website game details and comments (MQ + multithreading)
Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
The ultimate experience, the audio and video technology behind the tiktok
Visualization of residential house prices
Vite configure proxy proxy to solve cross domain
C# 的数据流加密与解密
Remember using Ali Font Icon Library for the first time
C language input and output (printf and scanf functions, putchar and getchar functions)
20222 return to the workplace
Go file operation
GDAL + ogr learning
Notes on common basic usage of eigen Library
re正则表达式
Calculation of fishing net road density
xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
MySQL auto start settings start with systemctl start mysqld
Logic regression principle and code implementation
JS high frequency interview questions
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资