如何删除多余的和前导的空格

问题描述 投票:0回答:2

我的任务要求是写这个函数(我可以有另一个函数):

void process (char input[], char output[]){

}

给定的

main()
是:

int main(){
    const int MAX_SIZE = 100;
    char input[] = " abc  def  ghi ";
    char output[MAX_SIZE];
    process(input, output);
    cout << input;
    return 0;
}

注意:可用的库是:

<cstring>
<iostream>

我写了两个函数:

void erase(char str[], int location){
    int len = strlen(str);
    for (int i = location; i < len; i++){
        str[i] = str[i+1];
    }
}

void process(char input[], char output[]){
    
    //eraseSpaces(str);
    int outIndex = 0, strIndex = 0;
    int n = strlen(input);

    for (int i = 0; i < n; i++){
        output[outIndex] = input[strIndex];

        if (input[i] == ' ' && input[i+1] == ' '){
            erase(str, i);
            i--;
        }
        
        if(input[0] == ' '){
            erase(str, 0);
        }
       
        if(input[n-1] == ' '){
            erase(str, n-1);
        }

        outIndex++;
        strIndex--;
    }
    output[outIndex] = '\0'; //ket thuc
}

然而,这并不奏效。我想要的结果是

"abc def ghi"
,结果不是我想要的

我该如何解决?

c++ whitespace fix-protocol
2个回答
0
投票

函数签名并不像您想象的那样。如果你只想使用静态大小的数组(连同它们的大小),我推荐这个:

template<std::size_t N, std::size_t M>
void process(char const (&input)[N], char (&output)[M])

但是,要处理类似 c 的字符串,只跳过 初始空格,即使不包括

<cstring>
.

,您也可以更轻松地完成此操作
void process(char const* input, char* output) {
    while (*input == ' ') ++input; // skip leading spaces
    while ((*output++ = *input++)) {} // copy rest, including '\0'
}

编辑:如果你还想跳过单词中的所有冗余空格,它会稍微复杂一些,但在结构上与之前的函数类似:

void process(char const* input, char* output) {
    auto const chunk = [](char const c) { return (c != ' ') & (c != 0);};
    do {
      while (*input == ' ') ++input; // skip superfluous spaces
      while (chunk((*output++ = *input++))) {} // copy until 0 or space (inclusive)
    } while (*(input-1));
}

0
投票

如果你想一次做所有的事情,但不知道哪里出了问题,我建议你一步一步来。 例如……

第一次尝试,只复制非空格字符。

void process( const char input[], char output[] )
{
    const char *pIn = input;
    char *pOut = output;
    while( *pIn )
    {
        //push only non space char
        if( *pIn != ' ' )
        {
            *pOut++ = *pIn;
        }
        ++pIn;
    }
    *pOut = '\0';
}

结果是

"abcdefghi"
。这是当前期望的结果。好。

现在我想在单词之间插入空格。 所以,添加流程来做吧。

void process( const char input[], char output[] )
{
    const char *pIn = input;
    char *pOut = output;
    while( *pIn )
    {
        if( *pIn != ' ' )
        {
            //push a space if previous input char is space
            if(
                   ( pIn != input ) //"previous" is existing
                && ( *(pIn-1)==' ' )    //"previous" is space
                )
            {   *pOut++ = ' ';  }

            *pOut++ = *pIn;
        }
        ++pIn;
    }
    *pOut = '\0';
}

结果变成

" abc def ghi"
.
哦,第一个空间!
当前代码是“在每个单词前添加空格”,而不是“在单词之间插入空格”!

我理解错了。因此修改

if
的条件:

void process( const char input[], char output[] )
{
    const char *pIn = input;
    char *pOut = output;
    while( *pIn )
    {
        if( *pIn != ' ' )
        {
            if(
                   ( pIn != input  &&  *(pIn-1)==' ' )  //previous input char is space
                && ( pOut != output )   //not first output
            )
            {   *pOut++ = ' ';  }

            *pOut++ = *pIn;
        }
        ++pIn;
    }
    *pOut = '\0';
}

现在我取得了好成绩

"abc def ghi"
。看起来不错。

然后用其他输入测试,如果有问题...修改...

© www.soinside.com 2019 - 2024. All rights reserved.