Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

counting new lines, new tabs and spaces in C using getchar()

I created a table that count new line, new words, sapces and tabs. I successfully count all of them but the problem is on the output:

I am trying to print one line only with the result with what I am am counting but instead the output is the whole count (the increment).

#include <stdio.h>
#define IN 1
#define OUT 0

int main()
{
    int c,nl,nw,nc,state;
    int newspace, tabchar;
    state = OUT;
    int id;

    nl = nw = nc = id = 0;
    newspace = tabchar = 0;
    printf("+===============================================================+
");
    printf("|                       WORDS, NEW LINE COUNTER                 |
");
    printf("+======+==========+==========+============+===========+=========+
");
    printf("|  ID  | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |
");
    printf("+===============================================================+
");

    while ((c = getchar()) != EOF)
    {
        ++nc;
        if(c == '
')
            ++nl;
        if(c == ' ' || c == '
' || c == '')
            state = OUT;
        if(c == ' ')
            newspace++;
        if(c == '')
            tabchar++;
        else if(state == OUT)
        {
            state = IN;
            ++nw;
        }
        id = nl + 1; 
        printf("|  %d  |     %d    |       %d   |       %d      |      %d     |     %d   |
", id, nl, nw, nc, newspace, tabchar);
    }
}

WRONG OUTPUT:

+===============================================================+
|                       WORDS, NEW LINE COUNTER                 |
+======+==========+==========+============+===========+=========+
|  ID  | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |
+===============================================================+
I am counting spaces and tab here
|  1  |   0    |      1   |      1   |     0   |     0   |
|  1  |   0    |      2   |      2   |     1   |     0   |
|  1  |   0    |      2   |      3   |     1   |     0   |
|  1  |   0    |      2   |      4   |     1   |     0   |
|  1  |   0    |      3   |      5   |     2   |     0   |
|  1  |   0    |      3   |      6   |     2   |     0   |
|  1  |   0    |      3   |      7   |     2   |     0   |
|  1  |   0    |      3   |      8   |     2   |     0   |
|  1  |   0    |      3   |      9   |     2   |     0   |
|  1  |   0    |      3   |      10   |     2   |     0   |
|  1  |   0    |      3   |      11   |     2   |     0   |
|  1  |   0    |      3   |      12   |     2   |     0   |
|  1  |   0    |      3   |      13   |     2   |     0   |
|  1  |   0    |      4   |      14   |     3   |     0   |
|  1  |   0    |      4   |      15   |     3   |     0   |
|  1  |   0    |      4   |      16   |     3   |     0   |
|  1  |   0    |      4   |      17   |     3   |     0   |
|  1  |   0    |      4   |      18   |     3   |     0   |
|  1  |   0    |      4   |      19   |     3   |     0   |
|  1  |   0    |      4   |      20   |     3   |     0   |
|  1  |   0    |      5   |      21   |     4   |     0   |
|  1  |   0    |      5   |      22   |     4   |     0   |
|  1  |   0    |      5   |      23   |     4   |     0   |
|  1  |   0    |      5   |      24   |     4   |     0   |
|  1  |   0    |      6   |      25   |     5   |     0   |
|  1  |   0    |      6   |      26   |     5   |     0   |
|  1  |   0    |      6   |      27   |     5   |     0   |
|  1  |   0    |      6   |      28   |     5   |     0   |
|  1  |   0    |      7   |      29   |     6   |     0   |
|  1  |   0    |      7   |      30   |     6   |     0   |
|  1  |   0    |      7   |      31   |     6   |     0   |
|  1  |   0    |      7   |      32   |     6   |     0   |
|  1  |   0    |      7   |      33   |     6   |     0   |
|  2  |   1    |      8   |      34   |     6   |     0   |

DESIRED OUTPUT:

+===============================================================+
|                       WORDS, NEW LINE COUNTER                 |
+======+==========+==========+============+===========+=========+
|  ID  | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |
+===============================================================+
I am counting spaces and tab here
|  2  |   1    |      8   |      34   |     6   |     0   |

What am I doing wrong? The output seems almost correct. It seems to me that the potential problem could be that the program does not understand properly the EOF in particular the following statement while ((c = getchar()) != EOF) but I am not sure why.

Thanks for pointing in the correct route for the solution.

question from:https://stackoverflow.com/questions/65877134/counting-new-lines-new-tabs-and-spaces-in-c-using-getchar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's actually working well but you're printing on every iteration. For your desired output, you should put the printf statement after the while block ends.

while ((c = getchar()) != EOF)
{
    ++nc;
    if(c == '
')
        ++nl;
    if(c == ' ' || c == '
' || c == '')
        state = OUT;
    if(c == ' ')
        newspace++;
    if(c == '')
        tabchar++;
    else if(state == OUT)
    {
        state = IN;
        ++nw;
    }
    id = nl + 1; 
    printf("|  %d  |     %d    |       %d   |       %d      |      %d     |     %d   |
", id, nl, nw, nc, newspace, tabchar);
}

should be:

while ((c = getchar()) != EOF)
{
    ++nc;
    if(c == '
')
        ++nl;
    if(c == ' ' || c == '
' || c == '')
        state = OUT;
    if(c == ' ')
        newspace++;
    if(c == '')
        tabchar++;
    else if(state == OUT)
    {
        state = IN;
        ++nw;
    }
    id = nl + 1; 
    
}

printf("|  %d  |     %d    |       %d   |       %d      |      %d     |     %d   |
", id, nl, nw, nc, newspace, tabchar);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...