1. 程式人生 > >Problem G: 部分複製字串

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’

}