strcmp
代码及运行结果:
#include<stdio.h>
int my_strcmp(char *p,char *q)
{
while(*p||*q)
{
if(*p++==*q++)
{
continue;
}
else
return *p>*q?1:-1;
}
return 0;
}
void main()
{
char a[]="hello";
char b[]="world";
char c[]="hello";
switch (my_strcmp(a,b)) //my_strcmp(a,c)
{
case 1 : printf("a>b\n");break;
case -1: printf("a<b\n");break;
case 0 : printf("a=b\n");break;
defalut:break;
}
}
strcpy
代码及运行结果:
#include<stdio.h>
void my_strcpy(char *p,char *q)
{
while(*p++=*q++)
;
}
void mian()
{
char a[]="hello";
char b[]="world";
my_strcpy(a,b);
puts(a);
}
strlen
代码及运行结果:
#include<stdio.h>
int my_strlen(char *q)
{
int len=0;
while(*q++)
len++;
return len;
}
void main()
{
char a[]="hello";
printf("%d\n",my_strlen(a));
}
strcat
代码及运行结果:
#include<stdio.h>
void my_strcat(char *p,char *q)
{
while(*p)
p++;
while(*q)
*p++=*q++;
}
void main()
{
char a[50]="hello";
char b[]="nihao";
strcat(a,b);
puts(a);
}