C语言OJ项目参考(2274)字符串插入

    xiaoxiao2026-05-18  16

    2274: 字符串插入

    Description 将字符串t插入到字符串s中,在位置pos后插入。不得使用字符串操作函数,输出组合成的字符串。 Input 输入两个字符串(t和s)和要插入的位置(pos) Output 输出组合后的字符串 Sample Input** qwe jij 3 Sample Output jijqwe 参考解答:

    #include<stdio.h> int main() { char t[100],s[100],r[200]; int pos,i=0,j=0; gets(t); gets(s); scanf("%d", &pos); //先复制s的pos位置前的字符 while(s[i]!='\0'&&i<pos) { r[i]=s[i]; i++; } //插入t while(t[j]!='\0') { r[i+j]=t[j]; j++; } //将s中剩余的复制 while(s[i]!='\0') { r[i+j]=s[i]; i++; } r[i+j]='\0'; puts(r); return 0; }
    最新回复(0)