|
Get 9,000+ Interview Questions with Answers in an eBook
C Source Codes List | Source Code Home
Program : Program to count the no. of words in an i/p text file.
/* Prg. to count the no. of words in an i/p text file. */
#include
#define NULL 0
FILE *fpt;
void main()
{
char name[20],c;
int nw=0;
clrscr();
printf("Enter the name of file to be checked:- ");
gets(name);
fpt=fopen(name,"r");
if (fpt==NULL)
{
printf("\nERROR - can/'t open file %s",name);
getch();
exit(0);
}
else
{
while ((c=getc(fpt))!=EOF)
{
switch(1)
{
case 1:
if (c==' ')
{
rikky: // do
// nw=nw+1-1;
while((c=getc(fpt))==' ');
if (c!=' ')
nw=nw+1;
if(c=='\n')nw--;
}
// case 3:
if(c=='\n'){
goto rikky;}
}
}
}
printf("\n\nThe no. of words in %s is %d. ",name,nw);
getch();
}
C Source Codes List | Source Code Home
|
|
|