繁体   English   中英

计算空格、制表符和换行符的程序

[英]program that counts blanks, tabs, and newlines

我正在阅读The C Programming Language 这是一个问题Write a program to count blanks, tabs, and newlines 现在我可以使用\n换行和\t制表符,但我第一次听说空白? 空格的真正含义,对于换行符和制表符:我编译了以下程序:

#include <stdio.h>

/*  program to count blanks, tabs, and newlines */
main (){
    long blanks, tabs, newlines, input;

    blanks = 0;
    tabs = 0;
    newlines = 0;
    input = 0;
    while ((input = getchar()) != EOF)
        if (input == '\n')
            ++newlines;
        else if (input == '\t')
            ++tabs;

    printf("Total newlines: %ld\nTotal Tabs: %ld", newlines, tabs);
}

空格 = 空格 ( ' ' )

尽管您的代码可以正常工作,但我强烈建议为 while 循环的主体添加{ }

大多数时候,空白只是一个空格。 您可能应该查看isblank()函数以帮助分类。

我确定它们指的是空格字符“”。

有关 ASCII 代码,请参见此处:

http://www.asciitable.com/

0x13 也是回车,可能要找那个? 换行符实际上并不那么简单,具体取决于文件的格式:

http://en.wikipedia.org/wiki/Newline

就像其他人所说的那样,您可能需要考虑使用来自

http://www.cplusplus.com/reference/clibrary/cctype/

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

int main()
{
printf("Type and Press Enter. CTRL-Z for EOF:\n");
int c;
int b = 0;
int t = 0;
int nl = 0;

while((c = getchar())!=EOF){

    putchar(c);
    if(c=='\t')
        ++t;

    if(c==' ')
        ++b;
    if(c=='\n')
        ++nl

}

printf("\n%d and %d\n",b,t,nl);


return 0;

}

您添加了一个 else if ,这里不需要,因为我们需要知道所有 3 个值。

请找到下面的代码,这将解决您的完整问题。

#include <stdio.h>

/* Count blank, tabs, and new lines */

main ()
{
    long c, b, t, l;    /* c for characters, b for blanks, t for tabs, l for lines */

    b = 0;
    t = 0;
    l = 0;

    while ( ( c = getchar() ) != EOF )
    {

        if ( c == '\n')
        ++l;
        if ( c == '\t')
        ++t;
        if ( c == ' ')
        ++b;
    }

    printf ("\n No of blanks : %d \n", b);
    printf ("\n No of tabs : %d \n", t);
    printf ("\n No lines in input: %d \n", l);
}

我认为空白是新行后跟新行

        #include<stdio.h>
        int main()
        {
            int c,nl,nb,nt;
            nl = 0;
            nb = 0;
            nt = 0;
            int flag =1;

            while((c = getchar()) != EOF){
                if(c == '\n')
                {
                    ++nl;
                    if(flag)
                    ++nb;
                    flag = 1;  
                }
                else
                flag = 0;     

                if(c == '\t')
                ++nt;     
            }
            printf("lines : %d, tabs: %d, blanks: %d", nl, nt, nb);     

            return 0;
        }

这个答案——受益于其他用户的贡献——只使用了练习 1-8 之前教授的材料。 当按下 Ctrl+z 时(我使用的是 Windows DOS),程序跳转到 printf 语句,打印答案,然后退出到 DOS 提示符。

#include <stdio.h>

/* Exercise 1-8.  Write a program to count blanks, tabs, and newlines. */
int main()
{
    int bl, tb, nl, c;

    bl = 0;
    tb = 0;
    nl = 0;
    while ((c = getchar()) != EOF) {
        if (c == ' ')
            ++bl;
        if (c == '\t');
            ++tb;
        if (c == '\n');
            ++nl;
        }
    printf("Total blanks: %d\n", bl);
    printf("Total tabs: %d\n", tb);
    printf("Total newlines: %d\n", nl);
}

当您按 Enter 键 5 次,按空格键 2 次,按 Tab 键 4 次,然后按 Ctrl+z,您将得到

总空白:2 总制表符:4 总换行符:5

然后是 DOS 提示符。

我认为的解决方案是:

#include <stdio.h>
main() {
    int c, nl, nt, nb;

    nl = 0;
    nt = 0;
    nb = 0;


    while((c = getchar()) != EOF){
        if (c == '\n')
            ++nl;

        if (c == '\t')
            ++nt;

        if (c == ' ')
            ++nb;
        }    

    printf("Blanks: %d\nNewlines: %d\nTabs: %d\n", nl, nt, nb);  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM