函数原型:int substr(char *dst, char *src, int start, int len);
从src开始位置向后偏移start个字符的位置开始,最多复制len个非空字符到dst的指定位置,dst必须以空字符结尾。函数返回存储于dst中的字符串长度。
#include <stdio.h>
int substr(char *dst, char *src, int start, int len)
{
int srcindex;
int dstindex = 0;
if (start >= 0 && len > 0)
{
for (srcindex = 0; srcindex < start && src[srcindex] != '\0'; srcindex++)
;
while (len > 0 && src[srcindex] != '\0')
{
dst[dstindex++] = src[srcindex++];
len -= 1;
}
}
dst[dstindex] = '\0';
return dstindex;
}
int main()
{
char src[64];
char dst[64];
sprintf(src, "hello world, 2015!");
substr(dst, src, 0, 50);
printf("%s\n", dst);
}