Hi

I have written the following code to count the number of lines in a file however I want to change this so I can find out how many words there are in the file and how many characters there are. Can anyone please help, thank you


#include <stdio.h>

/*Function Prototype*/
int nlines(char name[]);

void main(void)
{
char fname[100];
int lines;

printf("Program countlines lists the number of lines in a file.\n\n");

printf("Enter the file name: ");
scanf("%99s",fname);

lines=nlines(fname);

printf("There are %d lines in file %s",lines,fname);
}

/*Function Definition*/
int nlines(char name[])
{

FILE *ipfile;
char ch;
int nlines=0;

ipfile=fopen(name,"r");

if (ipfile==NULL)
{
printf("Error opening file \"%s\".",name);
}
else
{
do
{
ch=fgetc(ipfile);
if (ch!=EOF)
{
if(ch=='\n')
{
nlines+=1;
}
}
}
while (ch!=EOF);
fclose(ipfile);

nlines+=1;

}

return nlines;
}