Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to build a program that can tell how many words are in a file. #define NU

ID: 3644251 • Letter: T

Question

Trying to build a program that can tell how many words are in a file.

#define NULL 0
FILE *fpt;
void main()
{

char name[20],c;
int nw=0;
clrscr();
printf("Enter the name of file to be checked:- ");
gets(name);
fpt=fopen(name,"r");
if (fpt==NULL)
{
printf("ERROR - can/'t open file %s",name);
getch();
exit(0);
}
else
{
while ((c=getc(fpt))!=EOF)
{
switch(1)
{
case 1:
if (c==' ')
{
point: // do
// nw=nw+1-1;
while((c=getc(fpt))==' ');

if (c!=' ')
nw=nw+1;
if(c==' ')nw--;
}


// case 3:
if(c==' ')
{

goto point;
}

}
}
}
printf("The no. of words in %s is %d. ",name,nw);
getch();
}

Explanation / Answer

#define NULL 0 FILE *fpt; void main() { char name[20],c; int nw=0; clrscr(); printf("Enter the name of file to be checked:- "); gets(name); fpt=fopen(name,"r"); if (fpt==NULL) { printf("ERROR - can/'t open file %s",name); getch(); exit(0); } else { if (getc(fpt))==' ') nw++; printf("The no. of words in %s is %d. ",name,nw); getch(); }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote