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

入力; 引用符はスペースの代わりになります。:

Candy CCandy " " " "something

出力; スペースはまだそこにあります:

キャンディ キャンディ " " " "何か

4

5 に答える 5

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 に答える
1

まず、コードに多数の off-by-one エラーがあります。( の最大インデックス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 に答える
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 に答える