This program here is supposed to count the number of occurrences blanks, tabs and newlines within the respective text you type within the program. However, after I compile the program and run it on ubuntu's terminal, all I am able to do is punch in text, space, enter etc... endlessly unless I hit ctrl z or c which ends the program.
I do not know how to get the program to execute the program's printf() function, bottom portion of the program, to print or show the calculations for the number of blanks, tabs and newlines. There are no compiler or linker errors, but perhaps it is a bug.
The last few lines of the program starting from "if(c == EOF)" excluding the printf() portion are also unclear to me.
#include <stdio.h>
int main(void)
{
int blanks, tabs, newlines;
int c;
int done = 0;
int lastchar = 0;
blanks = 0;
tabs = 0;
newlines = 0;
while(done == 0)
{
c = getchar();
if(c == ' ')
++blanks;
if(c == '\t')
++tabs;
if(c == '\n')
++newlines;
if(c == EOF)
{
if(lastchar != '\n')
{
++newlines; /* this is a bit of a semantic stretch, but it copes
* with implementations where a text file might not
* end with a newline. Thanks to Jim Stad for pointing
* this out.
*/
}
done = 1;
}
lastchar = c;
}
printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
return 0;
}