#include #include void StrIns(char *s, int n, char *t) /* s:挿入先 n:挿入位置 t:挿入文字列 */ { char escape[30]; /* 文字列退避 */ char *area; /* 文字列退避エリアのポインタ */ char *tmp; /* s文字列退避用ポインタ */ int count = 0; /* escapeの退避文字数 */ int i; /* 挿入文字列カウンタ */ s = s + n; /* 挿入先sのポインタを挿入位置までずらす */ /* sの文字列はそのまま */ tmp = s; /* sの残った文字列の退避 */ area = escape; /* 文字列退避エリアのアドレスをコピー */ while(*tmp != '\0') { *area = *tmp; tmp++; area++; count++; } *area = '\0'; /* 文字列の挿入 */ while(*t != '\0') { *s = *t; s++; t++; } /* 残った文字列をつける */ for(i = 0; i < count; i++) { *s = escape[i]; s++; } *s = '\0'; } int main(void) { char s1[30]; /* 挿入先文字列s */ char s2[30]; /* 挿入文字列t */ int in; /* 挿入位置 */ memset(s1, '\0', sizeof(s1)); memset(s2, '\0', sizeof(s2)); printf("文字列を入力して下さい。:"); scanf ("%s", s1); printf("挿入文字列を入力して下さい。:"); scanf ("%s", s2); printf("挿入位置を入力して下さい。:"); scanf ("%d", &in); StrIns(s1, in, s2); printf("挿入後の文字列は:"); printf("%s", s1); return 0; }