Hi how would i return the value of word in the count words function to words in the main function im returning the wrong value. Am i returning it correctly
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int countWords(FILE *fp, char filech);
int main(void)
{
FILE *fp;
int i;
char cnt;
char ch;
int words=0, line=0, characters=0;
/*open in binary*/
if((fp = fopen("count.txt", "r")) ==NULL)
{
printf("cannot open\n");
exit(1);
}
while( (ch = fgetc(fp)) != EOF)
{
/*counts no characters in file*/
cnt++;
/*counts the no of lines in file*/
if(ch == '\n')
{
line = line +1;
}
characters = characters +1;
/*counts the number of words in a file*/
/*
if(ch == '\n' || ch == ' ' || ch == '.')
{
words = words +1;
}
*/
words = countWords(fp, ch);
}
printf("no chars is:%d\n",cnt);
printf("No of Lines %d\n", line);
printf("no of words are%d\n", words);
return 0;
}
int countWords(FILE *fp, char filech)
{
char word;
if(filech == '\n' || filech== ' ' || filech == '.')
{
word = word +1;
}
return (word);
}