Home > Net >  Is this a good way to remove spaces from a string in C? note, I am only using the <stdio.h> he
Is this a good way to remove spaces from a string in C? note, I am only using the <stdio.h> he

Time:10-21

int remove_whitespace(char* string, int size)   /* input parameters: the character array, size of that array   */
{
    int i = 0, j = 0, num = 0;   /* i - counter variable | j - counter variable | num - number of white spaces */

    for (i; i < size; i  )
    {
        if (string[i] == ' ')
        {
            for (j = i; j < size - 1; j  )
            {
                string[j] = string[j   1];
            }
            num  ;
        }
    }
    return num;   /* returns the number of white spaces */
}

CodePudding user response:

Count the 5 variables used by the function presented. Five! Are they all being incremented correctly? A pair of nested for() loops and an if() conditional. Are the indexes the correct index for the purpose??

Depending on the parameters passed, will whitespace be compacted but leave trailing characters in the array? Is this why the caller receives the "shrinkage" quantity?

Your title suggests "string", yet the code presented indicates an array of characters (without consideration for a C string terminating '\0'.) Taking the approach that you want to remove SP's (0x20) from a null terminated C string, the following will do that:

void stripSP( char *s ) {
    for( char *d = s; (*d = *s) != '\0'; s   ) d  = *d != ' ';
}

One line of code utilising two pointers.

You could pass/use a second parameter that is the character to be expunged,
or change the d = *d != ' '; to d = !isspace(*d); to compact-out any whitespace characters (incl. '\t', '\r' & '\n').

Obviously, this leads to possibilities of stripping out punctuation, or whatever you'd like removed. If the caller needs to know the new length of the string, the caller can invoke strlen().

It's one little line of source code, amenable to customising to suit your needs. For instance, the function could preserve and return the start address for chaining function calls.

CodePudding user response:

For starters the function should be declared like

size_t remove_char( char *string, size_t size, char c );

That is you should not write a separate function if you will want to remove another character instead of the space character ' '. Always try to write more general functions.

Also the function should return the size of the character array without the removed character.

Using nested for loops when all characters after the removed character are moved to the left is inefficient.

The function can be defined the following way

size_t remove_char( char *string, size_t size, char c )
{
    size_t n = 0;
 
    for ( size_t i = 0; i < size; i   )
    {
        if ( string[i] != c )
        {
            if ( n != i ) string[n] = string[i];
              n;
        }
    }

    return n;
}

Here is a demonstration program.

#include <stdio.h>

size_t remove_char( char *string, size_t size, char c )
{
    size_t n = 0;
 
    for ( size_t i = 0; i < size; i   )
    {
        if ( string[i] != c )
        {
            if ( n != i ) string[n] = string[i];
              n;
        }
    }

    return n;
}

int main( void ) 
{
    char s[10] = "H e l l o";

    size_t n = remove_char( s, sizeof( s ), ' ' ); 

    printf( "%zu: %s\n", n - 1, s );
}

The program output is

5: Hello

If you want to remove a character from a string then the function can be declared like

size_t remove_char( char *string, char c );

because you can determine the length of a string within the function by founding the terminating zero character '\0'.

  • Related