#include <stdio.h>
#include <string.h>
unsigned int mystrlen(char *str)
{
unsigned int count = 0;
while('\0' != *(str++))
{
count++;
}
return count;
}
/*str1:目的字符串 str2:源字符串 */
void mystrcpy(char *str1, char *str2)
{
char ch = ' ';
while('\0' != ch)
{
ch = *(str2++);
*(str1++) = ch;
}
*(str1++) = '\0';
}
/*
str1>str2 ret > 0
str1<str2 ret < 0
str1=str2 ret = 0
*/
int mystrcmp(char *str1, char *str2)
{
char ret1 = 0;
char ret2 = 0;
while(1)
{
ret1 = *(str1++);
ret2 = *(str2++);
printf("ret1:%c ret2:%c\n",ret1,ret2);
if('\0' == ret1 || '\0' == ret2)
{
return ret1-ret2;
}
if(ret1 != ret2)
{
return ret1-ret2;
}
}
//return 0;
}
void mystrcat(char *str1, char *str2)
{
while('\0' != *str1)
{
printf("str1:%c\n",*str1);
str1++;
}
while('\0' != *str2)
{
printf("str2:%c\n",*str2);
*(str1++) = *(str2++);
}
*(str1++) = '\0';
}
int main(void)
{
printf("========strlen()============\n");
printf("请输入数据:");
char str1[20] = "";
scanf(" %s",str1);
printf("strlen(str1):%zu\n",strlen(str1));
printf("mystrlen(str1):%u\n",mystrlen(str1));
printf("========strcpy()===========\n");
char str2_1[20] = "hello";
char str2_2[20] = "world";
char str2_3[20] = "";
strcpy(str2_3,str2_1);
printf("strcpy:%s\n",str2_3);
mystrcpy(str2_3,str2_2);
printf("mystrcpy:%s\n",str2_3);
printf("=========strcmp=============\n");
char str3_1[20] = "abd";
char str3_2[20] = "abe";
char ret3_1 = strcmp(str3_1,str3_2);
printf("strcmp==>ret3_1:%d\n",ret3_1);
if(ret3_1 > 0)
{
printf("%s大于%s\n",str3_1,str3_2);
}
else if(ret3_1 < 0)
{
printf("%s小于%s\n",str3_1,str3_2);
}
else
{
printf("%s等于%s\n",str3_1,str3_2);
}
char ret3_2 = mystrcmp(str3_1,str3_2);
printf("mystrcmp==>ret3_2:%d\n",ret3_2);
if(ret3_2 > 0)
{
printf("%s大于%s\n",str3_1,str3_2);
}
else if(ret3_2 < 0)
{
printf("%s小于%s\n",str3_1,str3_2);
}
else
{
printf("%s等于%s\n",str3_1,str3_2);
}
printf("========strcat==============\n");\
char str4_1[10] = "abc";
char str4_2[10] = "def";
strcat(str4_1,str4_2);
printf("strcat==>str4_1:%s\n",str4_1);
mystrcat(str4_1,str4_2);
printf("strcat==>str4_1:%s\n",str4_1);
return 0;
}
运行结果:
liang7212@Ubuntu1604:~/MyCodeProject/C/241011$ ./4.out
========strlen()============
请输入数据:acxd
strlen(str1):4
mystrlen(str1):4
========strcpy()===========
strcpy:hello
mystrcpy:world
=========strcmp=============
strcmp==>ret3_1:-1
abd小于abe
ret1:a ret2:a
ret1:b ret2:b
ret1:d ret2:e
mystrcmp==>ret3_2:-1
abd小于abe
========strcat==============
strcat==>str4_1:abcdef
str1:a
str1:b
str1:c
str1:d
str1:e
str1:f
str2:d
str2:e
str2:f
strcat==>str4_1:abcdefdef
标签:自写,str3,str1,strcat,str4,ret3,printf,字符串,20241011
From: https://blog.csdn.net/qq_64376986/article/details/142830111