Things You Will Need to Learn
Program Description
You will write a program to process a text file. Your program will consist of a series a functions to perform various tasks on the text file. You program will use getchar() and putchar() to do any necessary input/output to perform these tasks.The first function will output each line of text with a line number at the beginning of each line. For instance, given the input
This is the first line.
This is the second line.
this function would produce the output
1 This is the first line.
2 This is the second line.
The function prototype for this
function is
void line_num() /* Takes standard input or a redirected file and */
/* outputs to standard output the lines of the */
/* input prepended with line numbers. */
The second function will count how many times a given
letter appears in the input. It will return that count as well as print the
count to the output. The function has a parameter which tells the function which
letter it should look for in the input. The se should be case insensitive. For
example, if the letter passed to the function is "y", then the function will
count how many times the letter "y" or "Y" occurs in the input. Given the input,
You may wonder why the sky is blue. Then again, maybe you
will wonder
Why ask why?
a call to the function with the letter "y" as the parameter would produce the output
8
whereas a call to the function with the letter "i" as
the parameter would produce the output
3
The function prototype for this function should be
int cnt_let(char c) /* Takes standard input or a redirected file */
/* and counts the letter c (both upper and */
/* lower case). */
The third function will be used to encrypt
your file. It will use the system random number generator to perform the encryption.
It will take readable text (text that is unencrypted) and transform it to encrypted
text. The standard encryption would be adding the character to the random number
and then using "%" to mod it back t alphabetic value. This will be further explained
in class. You should definitely check out this sample
program which uses the random number generator to do character encryption.
Make sure you understand this program before attempting to incorporate this
concept into homework #3. Note: You should only encrypt alphabetic
characters. In other words the code given in the sample code is correct (although
you may choose an alternative encryption formula). Your main task to get this
part of the program to work is to create a function and generate the proper
interface with the main program. This really is the way secret messages are
protected. (An optional exercise is to write the corresponding decryption routine.)
The main() function will be written to test these routines. The special character "$" will signal which routine is to be invoked (called). The head of the input file will have a "$" followed by either a "1", a "2", a "3", or a "4". Seeing "$1" in the input means do line numbers (main hands calls the first function). Seeing "$2<character>" means count how often <character> appears in the input (main calls the second function). Seeing "$3" means encrypt the input (main calls the third function). Finally, seeing "$4" means do nothing (main ignores the input until the next "$" is encountered). So, encountering "$2b" in the input means to count all "b"'s (and "B"'s) until the next "$" is seen. The main program should terminate when it encounters EOF (end-of-file) in the input so you have to check for it in your program. If you're giving input to your program using the keyboard, you have to type "Ctrl-d" to signal EOF. If you're redirecting a inputfile into your program, you don't have to do anything special.
For a sample input and a sample output, click here . Recall redirection occurs when you type
% myprog < testfile
This uses the contents of testfile as the input to myporg.
Use the editor to create a testfile which tests your program comprehensively.
Do not just rely on the sample testfile given in the example.
Grading
Style -- 2 points total
Correctness -- 6 points total
Program execution -- 2 points total
Deductions
Deduct 3 points if the program does not compile.
(use "gcc <filename.c>")
Hints
$1Hi
world!
(EOF)
$ 1 H i '\n' w o r l d ! '\n' (EOF)
^
where the carat (^) represents the
file pointer.
#include <stdio.h>
int main(void)
{
char c;
c = getchar();
while ( c != )
c = getchar();
return 0;
}
When getchar() gets called the first time, c is assigned
the value '$', and the file pointer is advanced to the '1'. The first getchar()
call in the while loop assigns c the value '1' and advances the file pointer
to 'H'. Here are some of the iterations and the corresponding values of c after
getchar() is called:
$ 1 H i '\n' w o r l d ! '\n' (EOF)
^
getchar() assigns '$' to c and advances file pointer
$ 1 H i '\n' w o r l d ! '\n' (EOF)
^
getchar() assigns '1' to c and advances file pointer
$ 1 H i '\n' w o r l d ! '\n' (EOF)
^
getchar() assigns 'H' to c and advances file pointer
.
.
.
$ 1 H i '\n' w o r l d ! '\n' (EOF)
^
getchar() assigns '!' to c and advances file pointer
$ 1 H i '\n' w o r l d ! '\n' (EOF)
^
getchar() assigns '\n' to c and advances file pointer
$ 1 H i '\n' w o r l d ! '\n' (EOF)
^
getchar() assigns EOF to c
program terminates
FAQs
Star Solutions for Character Processing Program