Question
Complete the following program that Is, write the functions getInputs () countSpaces (), ainMax(), and printSummary (): def main () fname = get Inputs () # get the file name from the user infile = open (fname "r") # open the file for reading result = List # initialize result list to empty list for line in infile: num = countspaces line) #return the number of spaces in line resultList append (num) m M = minMax (resultList) #compute the min and max spaces per line print summary (m, M) #print the min and max spaces (including explanation)
Explanation / Answer
#include 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 : %d ", linecount); printf("Words : %d ", wordcount); printf("Characters : %d ", charcount); getchar(); return(0); }