大写字母计数、小写字母计数、空白计数和小写字母转换 - C

发布于 2024-12-29 05:41:57 字数 1112 浏览 1 评论 0原文

我对语言(C)有点陌生,但在我看来,我想做的事情非常简单。我确信我只是忽略了某些事情或遇到了一些逻辑错误。

正如标题所示,我希望我的程序做的就是计算小写字母、大写字母和空格的数量。作为附加功能,它还将输入的小写字母转换为大写字母。

#include <stdio.h>

int main(void){

    int iochar, numdigits=0, numlower=0, numupper=0, numwhites=0;

    printf("Please enter a phrase:\n\n");

    while((iochar=getchar())!=EOF) {
        if ((iochar=' ')||(iochar='\t')||(iochar='\n')){
            numwhites++;
            putchar(iochar);
        }
        else if((iochar>='0')&&(iochar<='9')) {
         numdigits++;
         putchar(iochar);
        }
        else if(('a'<=iochar)&&(iochar<='z')) {
            numlower++;
            putchar(iochar-32);
        } 
        else if(('A'<=iochar)&&(iochar<='Z')) {
            numupper++;
            putchar(iochar);
        }
        else 
           putchar(iochar); 
    }

    printf("%d white characters, %d digits, ",numwhites,numdigits);
    printf("%d lowercase have been converted to ",numlower);
    printf("uppercase and %d uppercase.\n",numupper);

    printf("\n\n");

    return 0;
}

I'm somewhat new to the language (C), but what i am trying to do is pretty simple imo. i'm sure im just neglecting something or running into some logical errors.

As the title suggests, all i want my program to do is count the # of lowercase letters, uppercase letters, and blank spaces. As an additional function, it also converts the lowercase letters entered into uppercase.

#include <stdio.h>

int main(void){

    int iochar, numdigits=0, numlower=0, numupper=0, numwhites=0;

    printf("Please enter a phrase:\n\n");

    while((iochar=getchar())!=EOF) {
        if ((iochar=' ')||(iochar='\t')||(iochar='\n')){
            numwhites++;
            putchar(iochar);
        }
        else if((iochar>='0')&&(iochar<='9')) {
         numdigits++;
         putchar(iochar);
        }
        else if(('a'<=iochar)&&(iochar<='z')) {
            numlower++;
            putchar(iochar-32);
        } 
        else if(('A'<=iochar)&&(iochar<='Z')) {
            numupper++;
            putchar(iochar);
        }
        else 
           putchar(iochar); 
    }

    printf("%d white characters, %d digits, ",numwhites,numdigits);
    printf("%d lowercase have been converted to ",numlower);
    printf("uppercase and %d uppercase.\n",numupper);

    printf("\n\n");

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅倚清风 2025-01-05 05:41:57
if ((iochar=' ')||(iochar='\t')||(iochar='\n'))

在此,您分配给 iochar 并且将始终为真。应该是一个比较:

if ((iochar==' ')||(iochar=='\t')||(iochar=='\n'))
if ((iochar=' ')||(iochar='\t')||(iochar='\n'))

in this you assign to iochar and will always be true. It should be a comparison:

if ((iochar==' ')||(iochar=='\t')||(iochar=='\n'))
三寸金莲 2025-01-05 05:41:57

请查看 ctype 中定义的 isupper(int c)islower(int c)isspace(int c) .h。您也许还可以受益于在同一文件中定义的 toupper(int c)tolower(int c)

另请注意 Sani 的答案,C 中的比较是 ==,而赋值是 =。此外,可以if 语句中进行赋值,这可能会导致出现像您这样的常见错误。在我看来,一个好的约定是这样编写比较:

if ('\n' == iochar)

这样,如果我犯了一个拼写错误并改为这样写:

if ('\n' = iochar)

我会在编译期间得到语法错误,而在运行时不会出现奇怪的行为。

Please look into isupper(int c), islower(int c), and isspace(int c) which are defined in ctype.h. You could perhaps also benefit from toupper(int c) and tolower(int c) which are defined in the same file.

Also note Sani's answer, comparison in C is == while assignment is =. Additionally, it is possible to assign within if statements, which make possible and even common errors such as yours. In my opinion, a good convention is to write comparisons like this:

if ('\n' == iochar)

This way, if I were to make a typo and write this instead:

if ('\n' = iochar)

I would get a syntax error during compilation and not weird behavior during runtime.

樱花坊 2025-01-05 05:41:57

我认为 Sani Huttenen 很好地解决了你的问题。我还建议您

putchar(iochar)

在结束时只打一次电话......这样会更清楚。

I think Sani Huttenen has solved your question pretty well. I'd also recommend you to call

putchar(iochar)

just once at the end of your bucle... It would be clearer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文