Can someone help me with this C program?! Its an intro C class so please try to
ID: 3692037 • Letter: C
Question
Can someone help me with this C program?! Its an intro C class so please try to keep it as simple as possible!! Thank you!!
Write a program, called word_ count.c, that receives one argument from the command line: word count file1.txt This operation implements the functionality of the UNIX wc command. Your program should print out the following information about the input file: ACER .The number of lines . The number of words . The number of characters Each line has no more than 100 characters. A line ends with the ' ' character except that the last line of the file ends with EOF. A word is defined as a string of letters (upper and lower case) and digits (0-9), starting with a letter or digit and ending before the first character that is not a letter or digit. A word has no more than 30 characters. For example, J.K. Rowling's Harry Potter will be considered as 6 words: J, K, Rowling, s, Harry, Potter.Explanation / Answer
#include<stdio.h>
#include<ctype.h>
int main(int argc, char *argv[])
{
FILE *f;
char ch;
int line=0,word=0,charcount = 0;
f=fopen(argv[1],"r");
while((ch=getc(f))!=EOF)
{
charcount++;
if(ch==' ')
line++;
if(isspace(ch)||ch==' '||ch==' ')
word++;
//putchar(ch);
}
fclose(f);
printf("No of characters=%d ",charcount);
printf("No of line=%d ",line);
printf("No of word=%d ",word);
return 0;
}
/*
SAmple run:
word.txt:
dswjnd; s qddqdkqNXQ
DKQD
DWQLKBQJxx
dm d ncsfwhxkX QW
DQWJDHQKDB
Output:
$ gcc FileWordLineCount.c
$ ./a.out word.txt
No of line=5
No of word=14