主函数
#include <stdio.h>
#include "mystr.h"
int main()
{
char* str="ert123";
printf("字符串的长度%d\n",my_strlen(str));
char str1[12];
char* str2="hello world";
my_strcpy(str1,str2);
printf("%s\n",str1);
printf("%d\n",my_strcmp(str1,str));
my_strcat(str2,str);
printf("%s\n",str2);
return 0;
}
改写函数
#include <stdio.h>
int my_strlen(char* str)
{
int count=0;
while(str[count]!='\0')
{
count++;
}
return count;
}
void my_strcpy(char* str1,char* str2)
{
int i;
for(i=0;str2[i]!='\0';i++)
{
str1[i]=str2[i];
}
str1[i]='\0';
}
int my_strcmp(char* str1,char* str2)
{
int res;
for(int i=0;str1[i]!='\0'||str2[i]!='\0';i++)
{
res=str1[i]-str2[i];
if(res)
{
break;
}
}
return res;
}
void my_strcat(char* str1,char* str2)
{
int i=0;
while(str1[i]!='\0')
{
i++;
}
int j=0;
while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
}
头文件
#ifndef __MY_STR__
#define __MY_STR__
int my_strlen(char* str);
void my_strcpy(char* str1,char* str2);
int my_strcmp(char* str1,char* str2);
void my_strcat(char* str1,char* str2);
#endif