Caesar

Problem to Solve
Supposedly, Caesar (yes, that Caesar) used to âencryptâ (i.e., conceal in a reversible way) confidential messages by shifting each letter therein by some number of places. For instance, he might write A as B, B as C, C as D, âŚ, and, wrapping around alphabetically, Z as A. And so, to say HELLO to someone, Caesar might write IFMMP instead. Upon receiving such messages from Caesar, recipients would have to âdecryptâ them by shifting letters in the opposite direction by the same number of places.
The secrecy of this âcryptosystemâ relied on only Caesar and the recipients knowing a secret, the number of places by which Caesar had shifted his letters (e.g., 1). Not particularly secure by modern standards, but, hey, if youâre perhaps the first in the world to do it, pretty secure!
Unencrypted text is generally called plaintext. Encrypted text is generally called ciphertext. And the secret used is called a key.
To be clear, then, hereâs how encrypting HELLO with a key of \(1\) yields IFMMP:
| plaintext | H | E | L | L | O | 
|---|---|---|---|---|---|
| + key | \(1\) | \(1\) | \(1\) | \(1\) | \(1\) | 
| = ciphertext | I | F | M | M | P | 
More formally, Caesarâs algorithm (i.e., cipher) encrypts messages by ârotatingâ each letter by \(k\) positions. More formally, if \(p\) is some plaintext (i.e., an unencrypted message), \(p_i\) is the \(i^{th}\) character in \(p\), and \(k\) is a secret key (i.e., a non-negative integer), then each letter, \(c_i\), in the ciphertext, \(c\), is computed as
\[c_i = (p_i + k)\space\%\space26\]wherein \(\%\space26\) here means âremainder when dividing by 26.â This formula perhaps makes the cipher seem more complicated than it is, but itâs really just a concise way of expressing the algorithm precisely. Indeed, for the sake of discussion, think of A (or a) as \(0\), B (or b) as \(1\), âŚ, H (or h) as \(7\), I (or i) as \(8\), âŚ, and Z (or z) as \(25\). Suppose that Caesar just wants to say Hi to someone confidentially using, this time, a key, \(k\), of 3. And so his plaintext, \(p\), is Hi, in which case his plaintextâs first character, \(p_0\), is H (aka 7), and his plaintextâs second character, \(p_1\), is i (aka 8). His ciphertextâs first character, \(c_0\), is thus K, and his ciphertextâs second character, \(c_1\), is thus L. Make sense?
In a file called caesar.c in a folder called caesar, write a program that enables you to encrypt messages using Caesarâs cipher. At the time the user executes the program, they should decide, by providing a command-line argument, what the key should be in the secret message theyâll provide at runtime. We shouldnât necessarily assume that the userâs key is going to be a number; though you may assume that, if it is a number, it will be a positive integer.
Demo
Specification
Design and implement a program, caesar, that encrypts messages using Caesarâs cipher.
- Implement your program in a file called caesar.cin a directory calledcaesar.
- Your program must accept a single command-line argument, a non-negative integer. Letâs call it \(k\) for the sake of discussion.
- If your program is executed without any command-line arguments or with more than one command-line argument, your program should print an error message of your choice (with printf) and return frommaina value of1(which tends to signify an error) immediately.
- If any of the characters of the command-line argument is not a decimal digit, your program should print the message Usage: ./caesar keyand return frommaina value of1.
- Do not assume that \(k\) will be less than or equal to 26. Your program should work for all non-negative integral values of \(k\) less than \(2^{31} - 26\). In other words, you donât need to worry if your program eventually breaks if the user chooses a value for \(k\) thatâs too big or almost too big to fit in an int. (Recall that anintcan overflow.) But, even if \(k\) is greater than \(26\), alphabetical characters in your programâs input should remain alphabetical characters in your programâs output. For instance, if \(k\) is \(27\),Ashould not become\even though\is \(27\) positions away fromAin ASCII, per asciitable.com;Ashould becomeB, sinceBis \(27\) positions away fromA, provided you wrap around fromZtoA.
- Your program must output plaintext:(with two spaces but without a newline) and then prompt the user for astringof plaintext (usingget_string).
- Your program must output ciphertext:(with one space but without a newline) followed by the plaintextâs corresponding ciphertext, with each alphabetical character in the plaintext ârotatedâ by k positions; non-alphabetical characters should be outputted unchanged.
- Your program must preserve case: capitalized letters, though rotated, must remain capitalized letters; lowercase letters, though rotated, must remain lowercase letters.
- After outputting ciphertext, you should print a newline. Your program should then exit by returning 0frommain.
Advice
How to begin? Letâs approach this problem one step at a time.
Pseudocode
First write, try to write a main function in caesar.c that implements the program using just pseudocode, even if not (yet!) sure how to write it in actual code.
Hint
Thereâs more than one way to do this, so hereâs just one!
int main(int argc, string argv[])
{
    // Make sure program was run with just one command-line argument
    // Make sure every character in argv[1] is a digit
    // Convert argv[1] from a `string` to an `int`
    // Prompt user for plaintext
    // For each character in the plaintext:
        // Rotate the character if it's a letter
}
Itâs okay to edit your own pseudocode after seeing ours here, but donât simply copy/paste ours into your own!
Counting Command-Line Arguments
Whatever your pseudocode, letâs first write only the C code that checks whether the program was run with a single command-line argument before adding additional functionality.
Specifically, modify main in caesar.c in such a way that, if the user provides no command-line arguments, or two or more, the function prints "Usage: ./caesar key\n" and then returns 1, effectively exiting the program. If the user provides exactly one command-line argument, the program should print nothing and simply return 0. The program should thus behave per the below.
$ ./caesar
Usage: ./caesar key
$ ./caesar 1 2 3
Usage: ./caesar key
$ ./caesar 1
Hints
- Recall that you can print with printf.
- Recall that a function can return a value with return.
- Recall that argccontains the number of command-line arguments passed to a program, plus the programâs own name.
Checking the Key
Now that your program is (hopefully!) accepting input as prescribed, itâs time for another step.
Add to caesar.c, below main, a function called, e.g., only_digits that takes a string as an argument and returns true if that string contains only digits, 0 through 9, else it returns false. Be sure to add the functionâs prototype above main as well.
Hints
- Odds are youâll want a prototype like:
    bool only_digits(string s);And be sure to include cs50.hatop your file, so that the compiler recognizesstring(andbool).
- Recall that a stringis just an array ofchars.
- Recall that strlen, declared instring.h, calculates the length of astring.
- You might find isdigit, declared inctype.h, to be helpful, per manual.cs50.io. But note that it only checks onecharat a time!
Then modify main in such a way that it calls only_digits on argv[1]. If that function returns false, then main should print "Usage: ./caesar key\n" and return 1. Else main should simply return 0. The program should thus behave per the below:
$ ./caesar 42
$ ./caesar banana
Usage: ./caesar key
Using the Key
Now modify main in such a way that it converts argv[1] to an int. You might find atoi, declared in stdlib.h, to be helpful, per manual.cs50.io. And then use get_string to prompt the user for some plaintext with "plaintext:  ".
Then, implement a function called, e.g., rotate, that takes a char as input and also an int, and rotates that char by that many positions if itâs a letter (i.e., alphabetical), wrapping around from Z to A (and from z to a) as needed. If the char is not a letter, the function should instead return the same char unchanged.
Hints
- Odds are youâll want a prototype like:
    char rotate(char c, int n);A function call like rotate('A', 1)or even rotate('A', 27)should thus return 'B'. And a function call likerotate('!', 13)should return '!'.
- Recall that you can explicitly âcastâ a charto anintwith(int), and anintto acharwith(char). Or you can do so implicitly by simply treating one as the other.
- Odds are youâll want to subtract the ASCII value of 'A'from any uppercase letters, so as to treat'A'as0,'B'as1, and so forth, while performing arithmetic. And then add it back when done with the same.
- Odds are youâll want to subtract the ASCII value of 'a'from any lowercase letters, so as to treat'a'as0,'b'as1, and so forth, while performing arithmetic. And then add it back when done with the same.
- You might find some other functions declared in ctype.hto be helpful, per manual.cs50.io.
- Odds are youâll find %helpful when âwrapping aroundâ arithmetically from a value like25to0.
Then modify main in such a way that it prints "ciphertext: " and then iterates over every char in the userâs plaintext, calling rotate on each, and printing the return value thereof.
Hints
- Recall that printfcan print acharusing%c.
- If youâre not seeing any output at all when you call printf, odds are itâs because youâre printing characters outside of the valid ASCII range from 0 to 127. Try printing characters temporarily as numbers (using%iinstead of%c) to see what values youâre printing!
Walkthrough
How to Test
Correctness
check50 cs50/problems/2024/summer/caesar
How to Use debug50
Looking to run debug50? You can do so as follows, after compiling your code successfully with make,
debug50 ./caesar KEY
wherein KEY is the key you give as a command-line argument to your program. Note that running
debug50 ./caesar
will (ideally!) cause your program end by prompting the user for a key.
Style
style50 caesar.c
How to Submit
Per Step 4 below, after you submit, be sure to check your autograder results. If you see SUBMISSION ERROR: missing files (0.0/1.0), it means your file was not named exactly as prescribed (or you uploaded it to the wrong problem).
Correctness in submissions entails everything from reading the specification, writing code that is compliant with it, and submitting files with the correct name. If you see this error, you should resubmit right away, making sure your submission is fully compliant with the specification. The staff will not adjust your filenames for you after the fact!
- Download your caesar.cfile by control-clicking or right-clicking on the file in your codespaceâs file browser and choosing Download.
- Go to CS50âs Gradescope page.
- Click Problem Set 2: Caesar.
- Drag and drop your caesar.cfile to the area that says Drag & Drop. Be sure it has that exact filename! If you upload a file with a different name, the autograder likely will fail when trying to run it. Ensuring you have uploaded files with the correct filename is your responsibility!
- Click Upload.
You should see a message that says âProblem Set 2: Caesar submitted successfully!â