We just learned about how to read and write to files with C. Write a program that uses Standard Input and Standard Output to take a message and encode it using a simple Caesar cipher. So, you should be able to redirect file input to your program:
> ./caesar_cipher_encode < ./my_message.txt > ./encoded_my_message.txt
and you should be able to encode keyboard input and print to the terminal:
(arbitrarily assumming you're shifting right by three letters)
> ./caesar_cipher_encode
> foo bar baz
> irr edu edc
We just learned about using multiple files. Let's say we want another program to do encoding using your Caesar cipher; we need to split the 'encode' function into a separate file to be shared.
Remember the steps for sharing code:
- Move the function declarations to a header file.
- Move the function definitions to a separate .c file.
- Require the header files in your main program file.
- When you compile the program, list all the .c files you need.
The other program to use your encoding function will read a file from a filename passed on the command line, and then write an output file with the encoded message. You will want to reuse the function performing the encryption between the two programs; put it in a separate file, and use a header file to include it in both your encoding programs.
- Make your program handle numbers and symbols by not changing them, using
isalpha - Handle lower and upper case letters using
islowerand/orisupper - Handle errors (see section 7.6 of "The C Programming Language")
Solution: Caesar Cipher