void mystrcpy(char *target, char *source) { while((*target = *source) != '\0') { source++; target++; } } void mynstrcpy(char *target, char *source, int n) { int i = 0; while((i < n) && (*target = *source) != '\0') { source++; target++; i++; } }
调用:
int main() { char * ptr = "hhahahahahahahha"; char * dest; dest = (char *)malloc(80 * sizeof(char)); // mystrcpy(dest, ptr); mynstrcpy(dest, ptr, 4); dest[4] = '\0'; printf("%s\n", dest); return 0; }
void myStrcat(char *dest, char *source) { while(*dest != '\0') { dest++; } while( (*dest = *source) != '\0') { source++; dest++; } }
void mynStrcat(char *dest, char *source, int n) { int i = 0; while(*dest != '\0') { dest++; } while( i < n && (*dest = *source) != '\0') { source++; dest++; } }