Question: Write a C program that takes an input text file (.txt) containing words separated by spaces, collects them into a two-dimensional array of chars (each
- Write a C program that takes an input text file (.txt) containing words separated by spaces, collects them into a two-dimensional array of chars (each row containing one word), and displays each word on a separate line in the Terminal Window. Populate the array character-by-character, one element at a time. You can identify spaces by checking the scanned characters ASCII value (a space has an ASCII value of 3210). Then, the program should reverse the order of the words (last-first, first-last, etc.), display them without spaces, and store them in a different text file. See the representative output.
Assume that the maximum character length of each word is 20, and there is a maximum of 1000 characters in the input text file. Set all unused elements in the char array to a value of zero (after you encounter the space character, write zero to the remaining elements in that row). When dealing with punctuation marks, assume that they are part of the word which they are adjacent to. Verify that the program successfully opens the input file; print an error message to the Terminal if unsuccessful. Be careful not to over-write the input file by using different names and file pointers for the input and output files.
The following line of code (which scans a single character from the input file and places it into the char variable ch) might be helpful:
while ((ch = getc(myfile)) != EOF)
The loop stops after the entire file has been scanned. The built-in constant EOF stands for End of File, and is included with the stdio.h library. A representative output is given below (the original text file contains the phrase: I'm a big boy now!):
I'm
a
big
boy
now!
The inverted version of the text without spaces is:
now!boybigaI'm
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
