Write a program in C which will open a text file named Story. You can create the
ID: 3571907 • Letter: W
Question
Write a program in C which will open a text file named Story. You can create the file using any text editor like notepad etc. and put random words. Make sure the text file is in the same directory of your C program. Next, you need to open the file and count the number of words and characters (no space) in the file. Finally, print the result into the output. Output: The file Story has the following Statistics: Words: 35 Character (no space): 129 Please include comment and indentation in program Write a program in C which will open a text tile name Story. You can create the file using any text editor like notepad etc. and put random words. Make sure the text file is in the same directory of your C program. Next, you need to open the file and count the number of words and characters |no space; in the file. Finally, print the result into the output. Output: The file .Story-fat has the following Statistics: Words: 35 Character |no space': 129 Please Include comment and indentation in program./Explanation / Answer
#include <stdio.h>
int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount, wordcount, charcount;
// Initialize counter variables
linecount = 0;
wordcount = 0;
charcount = 0;
// Prompt user to enter filename
printf("Enter a filename :");
gets(filename);
// Open file in read-only mode
fp = fopen(filename,"r");
// If file opened successfully, then write the string to file
if ( fp )
{
//Repeat until End Of File character is reached.
while ((ch=getc(fp)) != EOF) {
// Increment character count if NOT new line or space
if (ch != ' ' && ch != ' ') { ++charcount; }
// Increment word count if new line or space character
if (ch == ' ' || ch == ' ') { ++wordcount; }
// Increment line count if new line character
if (ch == ' ') { ++linecount; }
}
if (charcount > 0) {
++linecount;
++wordcount;
}
}
else
{
printf("Failed to open the file ");
}
printf("Lines Present in given file: %d ", linecount);
printf("Words Present in given file: %d ", wordcount);
printf("Characters Present in given file: %d ", charcount);
getchar();
return(0);
}
====
input in file
===========
this this,
Go pqrs you abcdefg this.
ouptut
==========
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.