Skip to content

Latest commit

 

History

History
57 lines (44 loc) · 2.47 KB

File metadata and controls

57 lines (44 loc) · 2.47 KB

Caesar Cipher

Reading and Writing Files

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

Code Reuse

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:

  1. Move the function declarations to a header file.
  2. Move the function definitions to a separate .c file.
  3. Require the header files in your main program file.
  4. 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.

Extra Credit

Solution: Caesar Cipher