有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串

有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串

解题思路: 当字符串指针移动到源字符串的第m位时,则开始向另一个缓冲区中写入剩下的数据

答案:

#include <stdio.h>
#include <string.h>

int main()
{
	char buf1[1024], buf2[1024];
	printf("Please enter a string: ");
	scanf_s("%s", buf1, 1024);
	int m;
	printf("Please enter a location to start copying: ");
	scanf_s("%d", &m);
	if (m < 0 || m > strlen(buf1)) {//检测输入的位置是否合法
		printf("Illegal location entered
");
		return -1;
	}
	char *ptr1 = buf1 + m; // 从第m个位置开始复制新数据
    char *ptr2 = buf2;
	while (*ptr1 != '') {
		*ptr2++ = *ptr1++;
	}
	*ptr2 = '';//不要忘了字符串结尾标志
	printf("%s
", buf2);
	system("pause");
	return 0;
}

有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串

原文地址:https://www.cnblogs.com/weiyidedaan/p/13275378.html