0

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

这是主要功能:

int main()
{
    bool program_start = 1;
    char user_sentence[MAX];

    cout<<"This program will take any sentence you write and correct any spacing\n"
        <<" 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 != '\0'); 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;
}

输入;引号代表空格。:

糖果CCandy""""什么的

输出; 空间仍然存在:

糖果ccandy""""什么的

4

5 回答 5

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]。您实际上并没有删除任何空格。

于 2014-05-07T21:34:47.510 回答
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
}
于 2014-05-07T21:57:49.087 回答
0

问题是当你在句子中找到一对空格时,你只是将一个复制到另一个而不删除任何东西,所以你仍然有一对空格。您想要的是在多余的空间之后复制所有内容以摆脱空间。就像是:

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 */
}
于 2014-05-07T21:46:32.607 回答
0

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

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

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

于 2014-05-07T21:40:13.330 回答
0

您遇到的另一个问题是,MAX即使字符可能更少,您也在移动字符。

例如,如果 MAX 是 1500,并且我输入了 9 个字符(8 个字母 + 1 个用于 nul 终止符)的句子“Help Me!”,那么您的程序将搜索 1500 个字符。

我建议您投资一些库函数,例如:
std::strchr-- 查找 C 字符串中的字符(例如空格)。
std::memmove-- 将字符从一个位置复制到另一个位置,尤其是在位置重叠的情况下。
std::strlen -- 返回字符串中的字符数。

于 2014-05-07T22:40:20.553 回答