Earlier, we have seen "C : Program to display tabs , backspaces visible in an unambiguous way", today we will write a small C program which counts number of characters , number of words and number of lines.
Program looks easy when they ask to count lines and numbers but how about words.
If input to stdin is character by character, then we can count characters easily. Also using '\n' , we can identify that line has encountered. Only problem is counting words.
So, we will use a mechanism to find if word has encountered. Lets introduce two states IN and OUT which states currently process word and the other says its out of word respectively.
Output of this program
In this program
Program looks easy when they ask to count lines and numbers but how about words.
If input to stdin is character by character, then we can count characters easily. Also using '\n' , we can identify that line has encountered. Only problem is counting words.
So, we will use a mechanism to find if word has encountered. Lets introduce two states IN and OUT which states currently process word and the other says its out of word respectively.
#include
#define IN 1
#define OUT 0
int main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while((c = getchar()) != EOF)
{
/* Increment number of characters */
++nc;
/* Increment number of lines if end of line is encountered */
if( c == '\n' )
{
++nl;
}
/* Anything other than character, mark it new word */
if( c == ' ' || c == '\n' || c == '\t' )
{
state = OUT; /* Its just completed processing a word */
} /* if new word, increment word count */
else if ( state == OUT )
{
state = IN;
++nw;
}
}
printf(" Number of Characters = %d \n",nc);
printf(" Number of lines = %d \n",nl);
printf(" Number of Words = %d \n",nw);
return 0;
}
Output of this program
mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out
This program counts
number of characters
number of words
number of lines
Number of Characters = 73
Number of lines = 4
Number of Words = 12
In this program
- We read input character by character
- First we increment the character (nc)
- We then check if it is a new line. If new line, we increment the character
- Now to increment word (collection of characters), we need to do multiple checks which signify end of word.
- If its new line or tab or space, we consider that as word and increment word
- Finally when you press Ctrl+D (in Linux), the program will display the number of characters, number of lines and number of words.
Hope this helped :)
No comments:
Post a Comment