1.strlen函数:统计字符串数量
int mystrlen(char str[]){
char* p = str;
int con = 0;
for(; *p != '\0'; p++)
con++;
return con;
}
2.strcpy函数:将第二个字符串复制给第一个
3.strcmp函数:返回两个字符串差值
4.strcat函数:将第二个字符串复制给第一个
#include<stdio.h>
#include<string.h>
int mystrlen(char str[]){
char* p = str;
int con = 0;
for(; *p != '\0'; p++)
con++;
return con;
}
char* mystrcpy(char* s1, char* s2){
char *p1 = s1;
char *p2 = s2;
while(*p2)
*p1++ = *p2++;
*p1 = '\0';
return s1;
}
int mystrcmp(char* s1, char* s2){
char *p1 = s1;
char *p2 = s2;
for(;*s1 == *s2;s1++,s2++ )
if(!*s1)
return 0;
return (*s1 - *s2);
}
char* mystrcat(char* s1, char* s2){
char *p1 = s1;
char *p2 = s2;
int i = 0, j = 0;
for(i = 0; *(p1+i) != 0; i++);
for(j = 0; ;i++,j++){
*(p1+i) = *(p2+j);
if(*(p2+j)==0)
break;
}
return s1;
}
int main(){
char str[100] = "abcde";
int con = mystrlen(str);
printf("%d",con);
char s1[100] = "abffef";
char s2[100] = "abcd";
mystrcpy(s1,s2);
printf("%s\n",s1);
//
// int res = mystrcmp(s1,s2);
// printf("%d",res);
mystrcat(s1,s2);
printf("%s\n",s1);
return 0;
}