Saturday, December 4, 2010

Word Count Using C Language

#include<stdio.h>
#include<conio.h>
#include<ctype.h>


int main()
{
int i=0;
char ch,*problem_in;
FILE *fp;
clrscr();


fp=fopen("problem.in","r");
if(fp!=NULL)
{
if(isalpha(*(problem_in+i-2)))
i=1;
else
i=0;


ch=getc(fp);
while(ch!=EOF)
{
ch=getc(fp);
switch(ch)
{
case ' ':
case '!':
case '?':
case ',':
case '.':
case ';':
case ':':i++;break;
}
}
fclose(fp);
}
else
printf("Error while opening the file problem.in!...");


fp=fopen("problem.out","w");
if(fp!=NULL)
{
printf("\nproblem.out\n");
printf("Number of words: %d",i);
fprintf(fp,"Number of words: %d",i);
fclose(fp);
}
else
printf("Error while opening the file problem.out!...");


getch();
return 0;
}

1 comment:

  1. hi was just seeing if you minded a comment. i like your website and the thme you picked is super. I will be back. free word counter

    ReplyDelete