Problem G: 部分复制字符串

Description
输入一个字符串,将该字符串从第m个字符开始的全部字符复制成另一个字符串。m有用户输入,值小于字符串的长度。要求编写一个函数mcopy(char *src, char *dst, int m)来完成。
Input
多组测试数据,每组输入一个数字m和字符串(字符串长度小于80)
Output
输出新生成的字符串
Sample Input
3 abcdefgh
6 This is a picture.
Sample Output
cdefgh
is a picture.

#include <stdio.h>
#include <string.h>
int main()
{
void copystr(char *,char *,int);
int m;
char str1[80],str2[80];
while(scanf("%d",&m)!=EOF)
{
gets(str1);
if(strlen(str1)>=m)
{
copystr(str1,str2,m);
printf("%s\n",str2);
}
}
return 0;
}

void copystr(char *p1,char *p2,int m)
{
p1=p1+m-1;
while(*p1!=’\0’)//判断条件,*p1是否为空即是否已结束
{
*p2=*p1;
p1++;
p2++;
}
*p2=’\0’;//最后一位要补上‘\0’

}

猜你喜欢

转载自blog.csdn.net/z2431435/article/details/84710533