H
hiteshthappa
hi
Can anyone please help me in finding the total number of words in a
file
I get the newlines, characters and blankspaces correctly but counting
words ia problem.I have tried many ways but it didnt help.
Here is my code......
#include <string.h>
#include <stdio.h>
main(int argc , char *argv[])
{
FILE *fp;
int ch;
int chr=0;
int totchr=0;
int bspc=0,totbspc=0;
int nline=0;
int word=0,totwrd=0;
int i=0;
fp=fopen(argv[1],"r");
if(argc != 2)
{
printf("\tInsufficient arguments\n");
printf("\tusage: wrd <filename>\n");
exit(0);
}
if(fp==NULL)
{
printf("Error In File Opening\n");
exit(0);
}
else
{
while((ch=fgetc(fp))!=EOF)
{
if(ch == ' ')
{
bspc++;
}
if(ch == '\n')
{
nline++;
}
chr++;
word++;
}
}
//word = bspc + nline;
totchr += chr;
totbspc += bspc;
totwrd += word;
printf("\nFile %s has\n", argv[1]);
printf("\n\twhite spaces are: %d\n", totbspc);
printf("\twords are: %d\n", totwrd);
printf("\tcharacters are: %d\n", totchr);
printf("\tlines are: %d\n\n", nline);
fclose(fp);
}
Can anyone please help me in finding the total number of words in a
file
I get the newlines, characters and blankspaces correctly but counting
words ia problem.I have tried many ways but it didnt help.
Here is my code......
#include <string.h>
#include <stdio.h>
main(int argc , char *argv[])
{
FILE *fp;
int ch;
int chr=0;
int totchr=0;
int bspc=0,totbspc=0;
int nline=0;
int word=0,totwrd=0;
int i=0;
fp=fopen(argv[1],"r");
if(argc != 2)
{
printf("\tInsufficient arguments\n");
printf("\tusage: wrd <filename>\n");
exit(0);
}
if(fp==NULL)
{
printf("Error In File Opening\n");
exit(0);
}
else
{
while((ch=fgetc(fp))!=EOF)
{
if(ch == ' ')
{
bspc++;
}
if(ch == '\n')
{
nline++;
}
chr++;
word++;
}
}
//word = bspc + nline;
totchr += chr;
totbspc += bspc;
totwrd += word;
printf("\nFile %s has\n", argv[1]);
printf("\n\twhite spaces are: %d\n", totbspc);
printf("\twords are: %d\n", totwrd);
printf("\tcharacters are: %d\n", totchr);
printf("\tlines are: %d\n\n", nline);
fclose(fp);
}