Search code examples
ccompressionwhitespacec-stringsremoving-whitespace

In C, trying to remove extra whiteSpace in char* and replace with only one space between words


I am trying to remove unnecessary whitespace from my char* for future use. Basically, I want to only have one space between words and remove any additional spaces, tabs, or new line characters in between words. The current code I have almost works I believe, but I am unable to read the memory of the individual characters I am storing in my array. Also any solution must not have a maximum character size so if dynamic memory allocation is needed that would need to be considered as well. Is there a way to get this working? Thanks

EDIT 1: trailing and leading spaces should also be removed. Thanks to @Vlad from Moscow for the clarification


int main()
{
    char* fileString1;
    fileString1=removeAdditionalWhiteSpace(fileString1);
}
char* removeAdditionalWhiteSpace(char* wordList) 
{
    char characterHolder;
    char* finalList[strlen(wordList)];
    char* delimeter = wordList;
    int i = 0;
    do 
    {
        finalList[i] += characterHolder;
        char* hasSpace = NULL;
        while (*delimeter == ' ' || *delimeter == '\n' || *delimeter == '\t')
        {
            if(*delimeter == ' ')
            {
                if(hasSpace==NULL)
                {
                    hasSpace = delimeter;
                    characterHolder = *delimeter;                           
                }
                else
                {
                    ++delimeter;
                }
            }
            else if(*delimeter == '\n' || *delimeter == '\t')
            {
                *delimeter = ' ';
                if(hasSpace==NULL)
                {
                    hasSpace = delimeter; 
                    characterHolder = *delimeter;                          
                }
                else
                {
                    ++delimeter;
                }
            }
        }
        hasSpace=NULL;
        characterHolder = *delimeter; 
        i++;      
    } 
    while( (*wordList++ = *delimeter++) );
    return *finalList;
}

Solution

  • Your function does not make sense and has undefined behavior.

    For example the variable characterHolder was not initialized and it is added to pointer finalList[i]

    char characterHolder;                 // <===
    char* finalList[strlen(wordList)];
    char* delimeter = wordList;
    int i = 0;
    do 
    {
        finalList[i] += characterHolder;  // <===
        //….
    

    If you need to remove redundant white spaces from a string including its leading and trailing white spaces then the function can look as it is shown in the demonstrative program below.

    #include <stdio.h>
    #include <ctype.h>
    
    char * remove_duplicate_spaces( char *s )
    {
        char *src = s, *dsn = s;
    
        while ( isspace( ( unsigned char )*src ) ) ++src;
    
        do
        {
            char c = *src;
    
            if ( isspace( ( unsigned char )c ) ) c = ' ';
    
            if ( c == ' ' )
            {
                while ( isspace( ( unsigned char ) *++src ) );
                if ( *src )
                {
                    *dsn++ = c;
                }
            }
    
            *dsn++ = *src;
        } while ( *src++ );
    
        return s;
    }
    
    int main(void) 
    {
        char s[] = "\t\tIt is\t\ta      very\nlong\nstring.\t\t";
    
        printf( "\"%s\"\n", s );
    
        printf( "\"%s\"\n", remove_duplicate_spaces( s ) );
    
        return 0;
    }
    

    Its output is

    "       It is       a      very
    long
    string.     "
    "It is a very long string."