#include<stdio.h>
int value;
int *my_strlen(char *);
int main(int argc, const char *argv[])
{
char s1[32] = "123456789";
int *q = my_strlen(s1);
printf("%d\n",*q);
return 0;
}
int *my_strlen(char *p){
char *ptemp = p;
while(*ptemp){
value++;
ptemp++;
}
return &value;
}
1.实现strlen函数功能
#include<stdio.h>
char *my_strcpy(char *p,char *q){
char *ptemp1 = p;
char *ptemp2 = q;
while(*ptemp2){
*ptemp1 = *ptemp2;
ptemp2++;
ptemp1++;
}
*ptemp1 = *ptemp2;
return p;
}
int main(int argc, const char *argv[])
{
char s1[] = "hello world";
char s2[] = "abcdhqyj";
my_strcpy(s1,s2);
printf("s1 = %s\n",s1);
printf("s2 = %s\n",s2);
return 0;
}
2.实现strcpy函数的功能
#include<stdio.h>
int *my_strcmp(char *p,char *q,int *x){
char *ptemp1 = p;
char *ptemp2 = q;
while(*ptemp2){
if(*ptemp1!=*ptemp2){
break;
}
ptemp1++;
ptemp2++;
}
*x = *ptemp1 - *ptemp2;
}
int main(int argc, const char *argv[])
{
char s1[32] = "abcdhqyj";
char s2[32] = "abcdhxlloworld";
int ret1 = 0;
my_strcmp(s1,s2,&ret1);
if(ret1 > 0){
printf("s1>s2\n");
}else if(ret1<0 ){
printf("s1<s2\n");
}else
printf("s1==s2\n");
return 0;
}
3.实现strcmp函数的功能