在 C 中,尝试删除 char* 中多余的空格并替换为单词之间只有一个 space

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

我正在尝试从我的 char* 中删除不必要的白色space 以备将来使用。基本上,我只想在单词之间有一个 space,并删除单词之间的任何其他 space、制表符或换行符。我相信我当前的代码几乎可以工作,但是我无法读取我存储在数组中的单个字符的内存。此外,任何解决方案都不得具有最大字符大小,因此如果需要动态内存分配,也需要考虑这一点。有没有办法让它工作?谢谢

编辑 1:还应删除尾随和前导 spaces。感谢来自莫斯科的@Vlad 的澄清


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

看起来超级复杂的男人。

这是一个从字符串中删除空格的简单函数:
" 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;
}

示例输出:

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

你的函数没有意义并且有未定义的行为。

例如变量characterHolder没有初始化,它被添加到指针finalList[i]

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

如果您需要从字符串中删除多余的空格,包括其前导空格和尾随空格,则该函数看起来就像下面的演示程序中所示。

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

它的输出是

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

简约方法:


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

最后说明:最后一个 while() 可能是一个 if(),因为只能有一个 trailig space.