自己设计实现strlen,strcat,strcpy,strcmp。
int my_strlen(char *a) { int count=0; while(*a) { a++; count++; } return count; } char* my_strcat(char *a,char *b) { char *c; c=a; while(*a) { a++; } while(*b) { *a++=*b++; } return c; } char* my_strcpy(char *c,char *b) { char *d; d=c; while(*b) { *c++=*b++; } return d; } int my_strcmp(char* a,char* b) { while((*a!=0)&&(*b!=0)) { a++; b++; } if(*a==0) return -1; else if(*b==0) return 1; else return 0; } int main() { int strlen,strcmp; char *p1,*p2; char arr1[20]="lalalalala"; char arr2[]=" huhuhu"; char arr3[20]="0"; strlen=my_strlen(arr1); printf("%d\n",strlen); p1=my_strcat(arr1,arr2); while(*p1) { printf("%c",*p1++); } printf("\n"); p2=my_strcpy(arr3,arr2); while(*p2) { printf("%c",*p2++); } printf("\n"); strcmp=my_strcmp(arr1,arr2); printf("%d\n",strcmp); return 0; }
时间: 2024-10-05 03:23:18