使用C进行资本化

时间:2018-02-04 08:41:54

标签: c

这是我的程序,我将输入的每个单词都大写。但问题是我得到的输出是不正确的。 这是我的输入

"A professor invites me to his “Black Lit” class; they’re
reading Larson’s Passing. One of the black
students says, “Sometimes light-skinned blacks
think they can fool other blacks,
but I can always tell,” looking
right through me.#"

这是我的输出

"A professor invites me to his “Black Lit” class; they’re
A Professor Invites Me To His "Black Lit" Class; They're reading Larson’s Passing. One of the black
Reading Larson's Passing. One Of The Black students says, “Sometimes light-skinned blacks
Students Says, "Sometimes Light-skinned Blacks think they can fool other blacks,
Think They Can Fool Other Blacks, but I can always tell,” looking
But I Can Always Tell," Looking right through me."

我的问题是如何获得更好的输出,对于每一行输入,我在每个输入行之后得到输出,并且在最后一行的输出中,我没有得到输出的程序? 非常感谢你。 我的代码在这里的中间代码下面:

    #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void read_and_capitalize() {
    int character;
    //Get the first character from the user
    printf("\n");

    character = getchar();
    if (isspace(character)) {
        while (isspace(character)) {
            character = getchar();

        }

        character = getchar();
    }

    while (character != '#') {
        if (isspace(character)) {
            printf(" ");
            while (isspace(character)) {
                character = getchar();
            }
            printf("%c", toupper(character));
            character = getchar();

        }
            else {
                printf("%c", character);
                character = getchar();
            }


        }
    }



    int main(void){
        read_and_capitalize();
        return EXIT_SUCCESS;
    }

1 个答案:

答案 0 :(得分:0)

一个问题是,您不打印换行符,因为isspace \n将为真。

因此,对代码进行简单更改可能会更改此行:

while (character != '#') {

进入这些行

while (character != '#') {
    if (character == '\n')
    {
            printf("%c", character);
            character = getchar();
            continue;
    }

那就是说,我认为你应该考虑重新设计来简化代码。通过引入一些状态变量来判断你是否有a)只是打印了一个空格/换行符b)是否需要在下一个打印时使用toupper 非空间角色。

它可能看起来像:

#include <stdio.h>
#include <stdlib.h>

void read_and_capitalize() 
{
    int character;
    int make_upper = 1;  // Explanation
                         // 1 means: "Don't echo a space"
                         //          "Call toupper on non-space characters"
                         //
                         // 0 means: "Echo a space"
                         //          "Don't call toupper on non-space characters"

    printf("\n");

    //Get the first character from the user
    character = getchar();
    while (character != '#') 
    {
        if (character == '\n')
        {
            printf("%c", character);
            make_upper = 1;
        }
        else if (isspace(character)) 
        {
            if (!make_upper) printf(" ");
            make_upper = 1;
        }
        else 
        {
            printf("%c", make_upper ? toupper(character) : character);
            make_upper = 0;
         }
         character = getchar();
     }
}

int main(void)
{
    read_and_capitalize();
    return EXIT_SUCCESS;
}