2

我正在阅读The C Programming Language。这是一个问题,它是写一个程序来计算空白、制表符和换行符。现在我可以将\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);
}
4

8 回答 8

5

空格 = 空格 ( ' ')

尽管您的代码正在运行,但我强烈建议您{ }为 while 循环的主体添加。

于 2012-06-21T14:21:33.907 回答
0

大多数时候,空白只是一个空格。您可能应该研究isblank()有助于分类的功能。

于 2012-06-21T14:20:57.070 回答
0

我确定它们的意思是空格字符''。

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

http://www.asciitable.com/

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

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

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

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

于 2012-06-21T14:22:47.270 回答
0
#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 个值。

于 2013-12-10T12:28:54.180 回答
0

请在下面找到代码,这将为您的完整问题提供解决方案。

#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);
}
于 2015-06-01T11:43:21.053 回答
0

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

        #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;
        }
于 2015-06-12T20:12:06.367 回答
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 提示符。

于 2019-01-09T04:16:57.477 回答
0

我认为的解决方案是:

#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);  }
于 2020-01-06T17:07:32.823 回答