/*write a program to insert a string into main string*/
#include <stdio.h>
int main()
{
char text[100], str[20], ins_text[100];
int i = 0, j = 0, k = 0, pos;
printf("enter main text: ");
gets(text);
printf("enter string to be inserted: ");
gets(str);
printf("enter the position to be inserted: ");
scanf("%d", &pos);
while (text[i] != '\0')
{
if (i == pos)
{
while (str[k] != '\0')
{
ins_text[j] = str[k];
j++;
k++;
}
}
else
{
ins_text[j] = text[i];
j++;
}
i++;
}
ins_text[j] = '\0';
printf("\n the new string is: ");
puts(ins_text);
return 0;
}
在航站楼
$ ./a.exe
enter main text: newsman
enter string to be inserted: paper
enter the position to be inserted: 4
The new string is: `newspaperan`
在上面的最终输出中:-"新字符串是:Newspperan",字母"m"缺失. 我认为这是因为在While循环中增加了"j++"; 有什么办法修好它吗?