简体   繁体   中英

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

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."

Man that looks super complicated.

Here's a simple function to remove whitespace from the string:
" This is a test \\t of some \\n Extra white space. "

#include <stdio.h>
#include <ctype.h>

void removeWS(char* text)
{
    char* d = text;

    while (isspace(*text)) ++text;

    while(*text)
    {
        *d = isspace(*text)? ' ' : *text;
        text++;
        while (isspace(*d) && isspace(*text)) ++text;
        if (*text) d++;
    }

    *d = *text;
}


int main(void) {
    char text[] = "  This   is a test  \t of some   \n Extra white  space.  ";

    removeWS(text);
    printf("%s\n", text);

    return 0;
}

Sample Output:

Success #stdin #stdout 0s 4284KB
This is a test of some Extra white space.

Minimalistic approach:


size_t delspaces(char * str)
{
size_t src, dst;
size_t cnt;

for (cnt=src=dst=0; str[dst] = str[src++]; ) {
        if (isspace(str[dst])) {
                if (dst && !cnt++) str[dst++] = ' '
                continue;
                }
        cnt=0;
        dst++;
        }

        // remove trailing spaces
while (dst && isspace(str[dst-1])) str[--dst] = 0;

        // return the string length of the resulting string
        // (which could be useful for the caller)
return dst;
}

Final note: the last while() could be an if() , since there can be only one trailig space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM