我需要从输入字符串中删除所有空格,并将新字符串复制到 deblank 函数中的另一个字符串。我收到 zsh 总线错误

问题描述 投票:0回答:2

当我尝试运行此代码时,它成功地要求我提供一个字符串并将其分配给 inpstr。当我调用 deblank 函数时,我得到“zsh 总线错误”任何帮助将不胜感激。

int main()
{
    //Problem 1 Code
    //Declare a string and assign user input to it with fgets and chomp it

    char inpstr[20];
    char outstr[20];
    printf("Enter a string: ");
    fgets(inpstr, 20, stdin);
    chomp(inpstr);

    //Call deblank to assign the copied string without whitespace to outstr
    deblank(inpstr, outstr);

    return 0;
}

void chomp(char word [])
{
    if(word[strlen(word) - 1] == '\n')
    {
        word[strlen(word) - 1] = '\0';
    }
}

void deblank(char *inpstr, char *outstr)
{
    //First we find the length of the string not including the null
    int length = strlen(inpstr);
    
    //declare a counting variable
    int o;

    //For loop to check each letter
    for(int i = 0; i < length; i++)
    {
        if(inpstr[i] != ' ')
        {
            outstr[o] = inpstr[i];
            o++;
        }
    }
    
}

我尝试了一些不同的方法来重新定义指针,但没有任何改变。遇到编译错误或另一个 zsh 总线错误。我删除了

int length = strlen(inpstr)
之前的代码并对其进行了测试,它运行正常。我相信错误始于
deblank
函数中的 for 循环。所有函数原型都在主函数之上键入,并且还包括库。

c c-strings function-definition removing-whitespace bus-error
2个回答
0
投票
  • 初始化o
  • 穿上 outstr
  • 在有额外条件的情况下使用以删除所有 , ' ' 最后
  • 添加包含和转发以使其编译
#include <stdio.h>
#include <string.h>

// Add forwards
void chomp(char word []);
void deblank(char *inpstr, char *outstr);

int main()
{
    //Problem 1 Code
    //Declare a string and assign user input to it with fgets and chomp it

    char inpstr[20];
    char outstr[20];
    printf("Enter a string: ");
    fgets(inpstr, 20, stdin);
    chomp(inpstr);

    //Call deblank to assign the copied string without whitespace to outstr
    deblank(inpstr, outstr);

    printf("The outstr: (%s)\n", outstr);
    return 0;
}

void chomp(char word [])
{
    // remove all  \n or ' '
    // there should be some length
    while(strlen(word) > 0 && (word[strlen(word) - 1] == '\n' ||  word[strlen(word) - 1] == ' ' ) )
    {
        word[strlen(word) - 1] = '\0';
    }
}

void deblank(char *inpstr, char *outstr)
{
    //First we find the length of the string not including the null
    int length = strlen(inpstr);

    //declare a counting variable
    int o=0;  // initialize it !!!

    //For loop to check each letter
    for(int i = 0; i < length; i++)
    {
        if(inpstr[i] != ' ')
        {
            outstr[o] = inpstr[i];
            o++;
        }
    }
    outstr[o]='\0';  // end the contructed with \0

}

祝你好运


0
投票

有几个问题

首先变量

o
没有初始化。

int o;

所以用它作为索引

outstr[o] = inpstr[i];

调用未定义的行为。

另一个问题是数组

outstr
将不包含字符串,因为您忘记将终止零字符 ' ' 附加到数组中存储的字符序列。

函数

deblank
可以用以下方式定义。请注意,您还应该删除制表符
'\t'
。在函数内使用函数
strlen
效率低下。该函数可以更简单,无需声明额外的变量。

#include <ctype.h>

//...

void deblank( const char *inpstr, char *outstr )
{
    do
    {
        if ( !isblank( ( unsigned char )*inpstr ) )
        {
            *outstr++ =  *inpstr;
        }
    } while ( *inpstr++ );
}

如果您只想删除空格字符

' '
则不需要标题
<ctype.h>
并且函数中的 if 语句将看起来像

        if ( *inpstr != ' ' )

最后是功能

chomp

void chomp(char word [])
{
    if(word[strlen(word) - 1] == '\n')
    {
        word[strlen(word) - 1] = '\0';
    }
}

如果将空字符串传递给它,也可以调用未定义的行为,因为表达式

strlen(word) - 1
会产生一个大的正数。

您可以直接在 main 中编写

,而不是使用您手动编写的函数
inpstr[ strcspn( inpstr, "\n" ) ] = '\0';
© www.soinside.com 2019 - 2024. All rights reserved.