#include <assert.h> int substr(char dst[], char src[], int start, int len) { int srcLen = strlen(src); int left = 0; assert(dst); assert(src); if (srcLen < start) { return -1; } while (start--) { src++; //指针向后偏移start } left = srcLen - start; if (left < len) { len = left; } int ret = len; while (len--) { *dst++ = *src++; } *dst = ‘\0‘; return ret; } int main() { char dest[10]; char *p = "bit-tech"; substr(dest, p, 4, 4); printf("%s\n", dest); return 0; }
时间: 2024-11-24 15:18:27