Question: Write a C program that finds and outputs ``words'' in a text file. The program should get the names of the input and output files
Write a C program that finds and outputs ``words'' in a text file. The program should get the names of the input and output files from its command-line arguments (not by prompting the user as most of our programs have done) and should print appropriate error messages if fewer than two command-line arguments are given or if it cannot open the specified files.
The input file can contain any characters -- alphabetic, numeric, punctuation, etc. -- but the output file should contain only the ``words'' (sequences of one or more alphabetic characters), each on a separate line, in lowercase. The program should also print (to standard output) the number of alphabetic characters and the number of total characters in the input file. So for example if file words-in.txt contains the following:
hello world HELLO AGAIN! and some numbers 1234 words,words,words,words.
calling the program with the command
./a.out words-in.txt words-out.txt
should print the line
54 alphabetic characters, 72 characters in all
and produce a file words-out.txt containing the following:
hello world hello again and some numbers words words words words
Since the logic for this program is a little tricky, I recommend that you start by writing a simpler but somewhat similar program that just converts alphabetic characters to lowercase, copies newline characters unchanged, and converts all other characters to spaces. If you can't figure out the full problem, you can turn in this simpler program for part credit.
Hints:
You will probably find the library functions isalpha and tolower useful.
You may find sample program simplefile-char.c helpful.
I strongly recommend that you read the input one character at a time rather than trying to read it into an array (which as of when this problem is being assigned we don't know about anyway!): This problem can be solved without arrays, and in fact I think they just complicate matters in some ways.
If you're having trouble figuring how to detect ``words'' in the input, you might think in terms of having a variable that represents whether the preceding character was alphabetic.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
