07 Jan

C Program to Count number of words & lines & characters without using array

C Program to count number of words & lines & characters without using array or pointers

#include <stdio.h>

#include<conio.h>

#define IN 1  //inside a word

#define OUT 0 //outside a word

main ()

{

int nl, nw, nc, state; //nl is number of line, nw for words and nc for characters

char c;

state = OUT;

nl=nw=nc=0; //initializing to 0

while ((c =getchar()) != EOF) // EOF is a character after pressing CTR+Z

{

++nc; //incrementing number of characters

if (c == ‘\n’)

++nl; //incrementing number of lines

if(c==’ ‘|| c==’\n’ ||c==’\t’)

state=IN; // changing state to inside word

if(state==IN) {

state=OUT; //changing state to outside

++nw; //incrementing number of words

}

}

printf (“%d %d %d”, nl, nw, nc);

getch();

}