重写strlen、strcpy、strcmp、strcat函数
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int i;
char str[20] = "hello";
char str1[20] = "";
char* pst1 = &str[0];
char* pst2 = &str1[0];
//strcpy
for(i=0;*(pst1+i)!='\0'&&i<20;i++)
{
*(pst2+i) = *(pst1+i);
}
*(pst2+i) = '\0';
printf("%s\n",str1);
//strlen
for(i=0;*(pst1+i)!='\0';i++){}
printf("%d\n",i);
//strcmp
char str2[20] = "world";
char* pst3 = &str2[0];
int j;
for(i=0;*(pst1+i)!='\0';i++){}
for(j=0;*(pst3+j)!='\0';j++){}
if((i-j) > 0)
{
printf("str>str1\n");
}
else if((i-j)<0)
{
printf("str1>str\n");
}
else
{
printf("str1=str2\n");
}
//strcat
j=0;
for(i=0;*(pst3+i)!='\0'&&j<19;i++)
{
for(j=0;*(pst1+j)!='\0';j++){}
*(pst1+j) = *(pst3+i);
j++;
}
*(pst1+j) = '\0';
printf("%s\n",str);
return 0;
}
//运行结果
ubuntu@ubuntu:1_22_test$ gcc string.c
ubuntu@ubuntu:1_22_test$ ./a.out
hello
5
str1=str2
helloworld