Criticizze
Criticizze

Reputation: 1

I need to remove all whitespaces from the input string and copy the new string to the other string in the deblank function. I am getting zsh bus error

When I try to run this code it successfully asks me for a string and assigns it to the inpstr. When I call the deblank function i get "zsh bus error" Any help would be highly appreciated.

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++;
        }
    }
    
}

I have tried a few different things to maybe redefine the pointers, but nothing changed. Either got a compiling error or another zsh bus error. I have deleted code up to the int length = strlen(inpstr) and tested it and it runs correctly. I believe the error begins with the for loop in the deblank function. All the function prototypes were typed above the main function and libraries were also included.

Upvotes: 0

Views: 71

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

There are several problems.

First of all the variable o was not initialized.

int o;

So using it as an index

outstr[o] = inpstr[i];

invokes undefined behavior.

Another problem is that the array outstr will not contain a string because you forgot to append the terminating zero character '\0' to the stored sequence of characters in the array.

The function deblank could be defined the following way. Pay attention to that you should also remove tab characters '\t'. And using within the function the function strlen is inefficient. The function can be more simpler without declaring additional variables.

#include <ctype.h>

//...

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

If you want to remove only the space character ' ' then the header <ctype.h> is not needed and the if statement within the function will look as

        if ( *inpstr != ' ' )

And at last the function chomp

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

also can invoke undefined behavior if an empty string will be passed to it because the expression strlen(word) - 1 will produce a big positive number.

Instead of using your manually written function you could just write in main

inpstr[ strcspn( inpstr, "\n" ) ] = '\0';

Upvotes: 2

S_IROTH
S_IROTH

Reputation: 235

  • Initialize o
  • Put \0 on end of outstr
  • Use while with extra conditions to remove all \n, ' ' at the end
  • Added includes and forwards to make it compile
#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

}

Good Luck

Upvotes: 0

Related Questions