Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

program that counts blanks, tabs, and newlines

Tags:

c

newline

tabs

I'm reading The C Programming Language. Here is a question which is saying Write a program to count blanks, tabs, and newlines. Now I can use \n for newlines and \t for tabs, but I am hearing first time about blanks! What it really mean by blanks? For the newlines and tabs, I have compiled following program:

#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);
}
like image 698
Santosh Kumar Avatar asked Nov 24 '25 16:11

Santosh Kumar


1 Answers

blanks = spaces (' ')

Though your code is working I strongly suggest adding { } for the body of the while loop.

like image 62
Karoly Horvath Avatar answered Nov 27 '25 07:11

Karoly Horvath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!