从字符数组/字符串中删除空格

Removing spaces from a char array/cstring?

本文关键字:删除 空格 字符串 字符 数组      更新时间:2023-10-16

我无法让我的程序删除在句子中发现的额外空格,例如空格后面有另一个空格。而我用来请求用户继续的while循环阻止了函数的运行。当我删除它时,程序的其余部分运行。

这是主要功能:

int main()
{
    bool program_start = 1;
    char user_sentence[MAX];
    cout<<"This program will take any sentence you write and correct any spacingn"
        <<" or capitalization (not proper nouns) mistakes you have.n";
    while(loop_continue(program_start))
    {
        input(user_sentence, MAX);
        uppercase_spacing(user_sentence);
        lowercase(user_sentence);
        cout<<user_sentence;
    }
    return 0;
}

我的问题是我的uppercase_spacing函数,我无法让它删除句子内的额外空格。

下面是我用来编辑句子的void函数:

void uppercase_spacing(char sentence[])
{
    int number = 0;
    if(isspace(sentence[0]))
    {
        for(int i = 0; i < MAX; i++)
        {
            sentence[i] = sentence[i + 1];
        }
        while(number<MAX)
        {
            if((isspace(sentence[number])) && (isspace(sentence[number+1])))
            {
                sentence[number] = sentence[number + 1];
            }
            number++;
        }
    sentence[0] = toupper(sentence[0]);
    }else{
        for(int index = 0; index<MAX;index++)
        {
            if((isspace(sentence[index])) && (isspace(sentence[index+1])))
                sentence[index]=sentence[index+1];
        }
        sentence[0] = toupper(sentence[0]);
    }
    return;
}
void lowercase(char sentence[])
{
    for(int i = 1; (i < MAX) && (i != ''); i++)
    {
        sentence[i] = tolower(sentence[i]);
    }
    return;
}

我在所有其他程序中都使用了这个布尔值,所以我认为问题出在程序的主要部分。

这是我的while循环使用的布尔函数:

bool loop_continue(bool another_round)
{
    bool again = another_round;
    char continue_loop = 'y';
    cout<<"Would you like to continue? Type y or Y for yes,n" 
        >>" or any other letter for no.n";
    cin>> continue_loop;
    if((continue_loop == 'y' )||(continue_loop == 'Y'))
    {
        cout<<"Okay, let's do this!"<<endl;
        again = 1;
    }else
    {
        cout<<"Goodbye."<<endl;
        again = 0;
    }
    return again;
}

输入;引号是用来代替空格的。

Candy Candy " " " "something

输出;空格还在:

糖果糖果" " " "something

首先,在您的代码中存在许多偏离1的错误。(char sentence[MAX]中的最大索引是MAX-1,并且在许多情况下,循环体与index=MAX-1一起运行并且访问sentence[index+1])。

第二,让我们深入了解你的函数在做什么…

for(int index = 0; index<MAX;index++)
{
    // if sentence[index] and sentence[index+1] are both spaces....
    if((isspace(sentence[index])) && (isspace(sentence[index+1])))
        // set sentence[index] to sentence[index+1] (??!)
        sentence[index]=sentence[index+1];
    // now proceed to the next character
}

你现在看到问题了吗?您正在将已知为空格的字符(sentence[index])设置为已知为空格的字符(sentence[index+1])。您实际上并没有删除任何空格。

void compress_spaces( char *src)
{
    char *dst = src;
    // skip leading spaces first
    while( isspace( *src ))
        src ++;
    int space = 0;  // a character recently copied was a space
    for( ; *src; src++ )
        if( isspace( *src ))
        {
            if( !space )    // append a space only after a non-space char
                *dst++ = *src;
            space = 1;
        }
        else
        {
            *dst++ = *src;  // apped a non-space char always
            space = 0;
        }
    if( space )    // truncate the terminating space
        dst--;
    *dst = 0;      // terminate the resulting string
}

没有发生任何事情的原因是因为当您在一行中找到两个空格时,您只是将一个空格替换为另一个空格。你所做的赋值实际上并没有移动数组的其余部分,它只是将[number]的值替换为[number+1]的值:

if((isspace(sentence[number])) && (isspace(sentence[number+1])))
{
    sentence[number] = sentence[number + 1];
}

当您在一行中找到像这样的两个空格时,您需要进入一个内部循环来找到第一个不是空格的字符,然后将该字符(以及所有后续字符)移到原始空格中。

问题是,当你在句子中找到一对空格时,你只是将其中一个复制到另一个上,而不删除任何内容,所以你仍然有一对空格。你要做的是把多余的空格后面的所有内容复制下来,去掉多余的空格。比如:

void uppercase_spacing(char sentence[]) {
    char *in, *out;
    in = out = sentence;
    while (*in) {  /* while there's more sentence... */
        while(isspace(*in)) in++;  /* skip any initial spaces */
        while (*in && !isspace(*in))  /* copy non-spaces */
            *out++ = toupper(*in++);   /* convert to upper case */
        *out++ = *in;  /* copy a single space or the end of the string */
    }
    if (--out > sentence && isspace(*--out))
        *out = 0;  /* trim off trailing space, if any */
}

另一个问题是,您正在移动MAX字符,即使可能有更少。

例如,如果MAX是1500,我输入句子"Help Me!",这是9个字符(8个字母+ 1为空结束符),您的程序将搜索1500个字符。

我建议你投资一些库功能,如:
std::strchr——在C-string中查找一个字符(如空格)。
std::memmove——将字符从一个位置复制到另一个位置,特别是当位置重叠时。
std::strlen——返回字符串中的字符数。