EsoMars
EsoMars

Reputation: 357

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("+===============================================================+\n");
    printf("|                       WORDS, NEW LINE COUNTER                 |\n");
    printf("+======+==========+==========+============+===========+=========+\n");
    printf("|  ID  | NEW LINE | NEW WORD | CHAR COUNT | NEW SPACE | NEW TAB |\n");
    printf("+===============================================================+\n");

    while ((c = getchar()) != EOF)
    {
        ++nc;
        if(c == '\n')
            ++nl;
        if(c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        if(c == ' ')
            newspace++;
        if(c == '\t')
            tabchar++;
        else if(state == OUT)
        {
            state = IN;
            ++nw;
        }
        id = nl + 1; 
        printf("|  %d  |     %d    |       %d   |       %d      |      %d     |     %d   |\n", 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.

Upvotes: 0

Views: 474

Answers (1)

SamuraiExx
SamuraiExx

Reputation: 106

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 == '\n')
        ++nl;
    if(c == ' ' || c == '\n' || c == '\t')
        state = OUT;
    if(c == ' ')
        newspace++;
    if(c == '\t')
        tabchar++;
    else if(state == OUT)
    {
        state = IN;
        ++nw;
    }
    id = nl + 1; 
    printf("|  %d  |     %d    |       %d   |       %d      |      %d     |     %d   |\n", id, nl, nw, nc, newspace, tabchar);
}

should be:

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

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

Upvotes: 1

Related Questions