Kochan插入字符串分段错误

Kochan插入字符串分段错误,c,C,我正在学习Kochan在C语言书中的编程,我正在做一个练习,这个练习要求函数在另一个字符串中插入一个字符串,函数调用包括插入字符串的位置 我已经编写了下面的代码,但每当我输入输入时,我都会收到一个分段错误。我认为这是因为“input”字符串被定义为用户输入的长度,然后insertString函数尝试向该字符串添加其他字符。我只是找不到一种方法来定义足够大的字符串,以便能够容纳更多的字符。你认为这就是我收到分割错误的原因吗?有没有其他办法解决这个问题 #include<stdio.h>

我正在学习Kochan在C语言书中的编程,我正在做一个练习,这个练习要求函数在另一个字符串中插入一个字符串,函数调用包括插入字符串的位置

我已经编写了下面的代码,但每当我输入输入时,我都会收到一个分段错误。我认为这是因为“input”字符串被定义为用户输入的长度,然后insertString函数尝试向该字符串添加其他字符。我只是找不到一种方法来定义足够大的字符串,以便能够容纳更多的字符。你认为这就是我收到分割错误的原因吗?有没有其他办法解决这个问题

#include<stdio.h>
#include <string.h>


insertString(char input[], const char insert[], int position)
{
int i, j;
char temp[81];

j = strlen(input);

for(i = 0; i < position - 1; i++)
{
    temp[i] = input[i];
}

for(j = 0; insert != '\0'; i++, j++)
{
    temp[i] = insert[j];
}

for(j = i - j; input != '\0'; i++, j++)
{
    temp[i] = input[j];
}

for(i = 0; temp[i] != '\0'; i++)
{
    input[i] = temp[i];
}

input[i] = '\0';
}

void readLine(char buffer[])
{
char character;
int i = 0;

do
{
    character = getchar();
    buffer[i] = character;
    i++;
}
while(character != '\n');

buffer[i - 1] = '\0';
}

int main(void)
{
char input[81];
char insert[81];
int position;

printf("Enter the first string: ");
readLine(input);

printf("Enter the insert string: ");
readLine(insert);

printf("Enter placement position int: ");
scanf("%i", &position);

insertString(input, insert, position);

printf("The adjusted string is %s\n", input);

return 0;
}
#包括
#包括
insertString(字符输入[],常量字符插入[],整数位置)
{
int i,j;
炭温[81];
j=strlen(输入);
对于(i=0;i
可能还有其他原因,但以下片段肯定会崩溃:

for(j = 0; insert != '\0'; i++, j++)
{
    temp[i] = insert[j];
}

原因是-由于插入将不会增加或操纵-这是一个无休止的循环,将“无限”长时间写入temp。一旦超过其长度
80
(或稍晚一点),它将崩溃。我想你的意思是(j=0;insert[j]!='\0';I++,j++),对吧?

检查
函数中的所有
循环条件。例如:

for(j = 0; insert != '\0'; i++, j++)
{
    temp[i] = insert[j];
}

是无限循环。因此,您可以在
temp
数组边界之外访问内存。它会导致UB和分段错误。看起来您需要
插入[j]!='\这里的条件。

我对这本书很熟悉。作者斯蒂芬·科昌(Stephen Kochan)有一个网站,上面有奇数章末练习的答案

该网站位于classroomm.com,但你需要四处看看才能找到相关信息

以下是该网站上与此练习相关的信息:

C语言编程,练习10-7(第三版)和9-7(第四版)

/*将字符串s从i开始插入字符串源 此函数使用定义的stringLength函数 在本章中

注意:此函数假定源足够大 存储插入的字符串(危险!)*/

/*我们已经腾出了空间,现在在 插入点*/

for ( j = 0;  j < lenS; ++j )
    source [j + i] = s[j];
}
(j=0;j 源[j+i]=s[j]; }

insertString函数中的某个地方出错,超出了界限。顺便说一下,insertString函数不是以void这个词开头的

如果我替换为练习编写的insertString函数,那么程序就会工作

#include<stdio.h>
#include <string.h>

void insertString (char source[], const char s[], int start)
{
    int stringLength (const char s[]);

    int lenSource = strlen (source);
    int lenString = strlen (s);
    int i;

    if ( start > lenSource ) {
        printf ("insertion point exceeds string length\n");
        return;
    }

    // move the characters in the source string which are above the
    // starting point (including the terminating null character) to make
    // room for the new characters; to avoid overwriting characters the
    // process begins at the end of the string

    for ( i = lenSource; i >= start; --i )
        source[i + lenString] = source[i];

    // insert new characters 

    for ( i = 0; i < lenString; ++i )
       source[start + i] = s[i];
}

void readLine(char buffer[])
{
char character;
int i = 0;

do
{
    character = getchar();
    buffer[i] = character;
    i++;
}
while(character != '\n');

buffer[i - 1] = '\0';
}

int main(void)
{
char input[81];
char insert[81];
int position;

printf("Enter the first string: ");
readLine(input);

printf("Enter the insert string: ");
readLine(insert);

printf("Enter placement position int: ");
scanf("%i", &position);

insertString(input, insert, position);

printf("The adjusted string is %s\n", input);

return 0;
}
#包括
#包括
void insertString(字符源[],常量字符s[],int start)
{
int stringLength(const char s[]);
int lenSource=strlen(源);
int lenString=strlen(s);
int i;
如果(启动>源){
printf(“插入点超过字符串长度\n”);
返回;
}
//移动源字符串中位于
//要生成的起始点(包括终止的空字符)
//为新字符留出空间;避免覆盖字符
//进程从字符串的末尾开始
对于(i=lenSource;i>=start;--i)
源[i+lenString]=源[i];
//插入新字符
对于(i=0;i
谢谢您的帮助。我遵循了其他答案,意识到我忘记在几个for循环中指定数组的[I]。现在它似乎工作得更好了一些,但是这个例子看起来需要的代码要少得多——然而,我真的无法理解它的逻辑。为什么从插入点向后复制字符与向前复制有什么区别?那么,在插入部分之后,源字符串的其余部分是如何被复制到的呢?您只需要2个for循环,而不需要临时字符串。第一个for循环通过向上移动插入点上方的源部分(包括此处由+表示的终止空字符),在源中腾出空间。所以“错误的儿子+”变成了“错误的儿子+”。然后第二个for循环在插入点插入“per”——“错误的sonson+”变成“错误的person+”。当你在字符串中向上移动“son+”时,它会向后移动,这样字符就不会被覆盖。哦,我明白了,谢谢你的解释。然而,当我试图实现上述代码时,我仍然收到一个分段错误。我假设这是因为用户输入将source[]定义为某个长度,然后w
#include<stdio.h>
#include <string.h>

void insertString (char source[], const char s[], int start)
{
    int stringLength (const char s[]);

    int lenSource = strlen (source);
    int lenString = strlen (s);
    int i;

    if ( start > lenSource ) {
        printf ("insertion point exceeds string length\n");
        return;
    }

    // move the characters in the source string which are above the
    // starting point (including the terminating null character) to make
    // room for the new characters; to avoid overwriting characters the
    // process begins at the end of the string

    for ( i = lenSource; i >= start; --i )
        source[i + lenString] = source[i];

    // insert new characters 

    for ( i = 0; i < lenString; ++i )
       source[start + i] = s[i];
}

void readLine(char buffer[])
{
char character;
int i = 0;

do
{
    character = getchar();
    buffer[i] = character;
    i++;
}
while(character != '\n');

buffer[i - 1] = '\0';
}

int main(void)
{
char input[81];
char insert[81];
int position;

printf("Enter the first string: ");
readLine(input);

printf("Enter the insert string: ");
readLine(insert);

printf("Enter placement position int: ");
scanf("%i", &position);

insertString(input, insert, position);

printf("The adjusted string is %s\n", input);

return 0;
}