6-10 使用函數實現字符串部分復制 (20分)
本題要求編寫函數,將輸入字符串t中從第m個字符開始的全部字符復制到字符串s中。
函數接口定義:
void strmcpy( char *t, int m, char *s );
函數strmcpy
將輸入字符串char *t
中從第m
個字符開始的全部字符復制到字符串char *s
中。若m
超過輸入字符串的長度,則結果字符串應為空串。
裁判測試程序樣例:
#include <stdio.h> #define MAXN 20 void strmcpy( char *t, int m, char *s ); void ReadString( char s[] ); /* 由裁判實現,略去不表 */ int main() { char t[MAXN], s[MAXN]; int m; scanf("%d\n", &m); ReadString(t); strmcpy( t, m, s ); printf("%s\n", s); return 0; } /* 你的代碼將被嵌在這里 */
輸入樣例:
7
happy new year
輸出樣例:
new year
void strmcpy( char *t, int m, char *s )
{
int i=0;
int count=0;
for(i=m-1;t[i]!='\0';i++)
{
s[count++]=t[i];
}
s[count]='\0';
}