1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int my_strlen(char *str); 5 char *my_strcpy(char *dest, char *src); 6 char *my_strcat(char *dest, char *src); 7 int my_strcmp(char *str1,char *str2); 8 9 int main() 10 { 11 char dest[32] = "chen"; 12 char src[32] = "chengha"; 13 int res; 14 //printf("strlen(%s) = %d\n",str,my_strlen(str)); 15 res = my_strcmp(dest,src); 16 printf("res = %d\n",res); 17 //puts(dest); 18 system("pause"); 19 return 0; 20 } 21 22 int my_strlen(char *str) 23 { 24 int len = 0; 25 while(*str != ‘\0‘) 26 { 27 len++; 28 str++; 29 } 30 return len; 31 } 32 33 char *my_strcpy(char *dest, char *src) 34 { 35 char *address = dest; 36 while((*dest = *src) != ‘\0‘) 37 { 38 dest++; 39 src++; 40 } 41 return address; 42 } 43 44 char *my_strcat(char *dest, char *src) 45 { 46 char *address = dest; 47 while(*dest != ‘\0‘) 48 dest++; 49 while((*dest = *src) != ‘\0‘) 50 { 51 dest++; 52 src++; 53 } 54 return address; 55 } 56 57 int my_strcmp(char *str1,char *str2) 58 { 59 for(; *str1 == *str2; str1++, str2++) 60 if(*str1 == ‘\0‘) 61 return 0; 62 return *str1 - *str2; 63 }
时间: 2024-10-13 06:44:32