本題要求編寫函數,將輸入字符串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
1 #include <string.h> 2 3 void strmcpy( char *t, int m, char *s ){ 4 int n=strlen(t); 5 if(m>n){ 6 *s=NULL; 7 } 8 else{ 9 for(int i=0;i<=(n-m);i++){ 10 /* 11 這里拿字符1一試,只有一個字符1,m為1 12 i=0,n-m=1,遍歷一次,要得到s[0]=t[0]的結果,因此t的下標是m+i-1 13 */ 14 s[i]=t[m+i-1]; 15 } 16 } 17 }