1 #include2 3 int strlen_1(char *p); 4 5 int main(void) 6 { 7 char a[] = "happy"; 8 printf("该字符串的长度为%d\n",strlen_1(a)); 9 10 return 0; 11 } 12 13 int strlen_1(char *p) 14 { 15 int i; 16 for (i = 0; *p++ != '\0'; i ++) { 17 } 18 return i; 19 20 } 21
1 #include2 3 void strcpy_1(char *p, char *q); 4 5 int main(void) 6 { 7 char a[] = "happy"; 8 char b[] = "newyear"; 9 10 strcpy_1(a, b); 11 printf("%s\n",a); 12 13 return 0; 14 } 15 16 void strcpy_1(char *p, char *q) 17 { 18 int i; 19 for (i = 0; *p || *q != '\0'; i++) { 20 *p = *q; 21 p++; 22 q++; 23 } 24 } 25
1 #include2 3 int strcmp_1(char *p, char *q); 4 5 int main(void) 6 { 7 char a[] = "happynew"; 8 char b[] = "happyyear"; 9 10 printf("%d\n",strcmp_1(a, b)); 11 12 return 0; 13 } 14 15 int strcmp_1(char *p, char *q) 16 { 17 do { 18 p++; 19 q++; 20 if (*p == *q && *p == '\0') { 21 return 0; 22 } 23 } while(*p == *q); 24 return *p - *q; 25
1 #include2 3 void strcat_1(char *p, char *q); 4 5 int main(void) 6 { 7 char a[20] = "happy"; 8 char b[20] = "newyear"; 9 10 strcat_1(a, b); 11 printf("%s\n", a); 12 return 0; 13 } 14 15 void strcat_1(char *p, char *q) 16 { 17 do { 18 p++; 19 } while(*p != '\0'); 20 do { 21 *p = *q; 22 p++; 23 q++; 24 } while(*q != '\0'); 25 } 26
26 } 27