当前位置:网站首页>用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
边栏推荐
猜你喜欢
Type conversion in C #
Understanding and installing MySQL
[leetcode 54] spiral matrix
MySQL advanced query
A sharp tool to improve work efficiency
Generation of verification code
Addition, deletion, query and modification of data
Export the articles written in CSDN to PDF format
Addition, deletion, modification and query of MySQL table
RPC must know and know
随机推荐
几行代码教你爬取LOL皮肤图片
POJ - 2955 brackets interval DP
Stability building best practices
SQL -- data filtering and grouping
Usage scenario of copyonwritearraylist
Addition, deletion, modification and query of MySQL table
Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
Import of data
[leetcode 383] ransom letter
MySQL best practices for creating tables
Collection and map thread safety problem solving
检测技术与原理
[untitled] database - limit the number of returned rows
線性代數第二章-矩陣及其運算
基于pygame库编写的五子棋游戏
Doomsday (simple computational geometry)
Export of data
Example of reentrant lock thread waiting to wake up
Techniques et principes de détection
word排版遇到的格式问题