The solution to CS50 Psets 2 Caesar Problem (2022)

For this problem, we have to implement a program that encrypts messages using Caesar’s cipher. So, I’m sharing my approach and solution to the CS50 Psets 2 Caesar problem. But before that, we need to understand what is Caesar’s cipher. So first, let’s find out that.

What is Caesar Cipher, the background

This cipher system is used to ‘encrypt’ confidential messages by shifting each letter by some number of places. The following example will show you how this Ceasar cipher system works.

CS50 2022 psets 2 ceasar

In this example, to say HELLO to someone, Causar might write IFMMP. After receiving such a message from Causar, the receiver must decrypt them by shifting letters in the opposite direction in the same number of places. That means using the key. So if someone has the key, the message can be decrypted.

You can use the following algorithm for encrypting messages. This algorithm encrypts messages by rotating each letter by k positions. Here, p is some plaintext usually, an unencrypted message, Pi is the ith character in p, and k is a secret key ( this should be a non-negative integer), then each letter, ci, in the ciphertext, c is computed as the following algorithm. %26 means remainder when dividing by 26.

c i = ( p i + k ) % 26

Here is my Solution to CS50 Psets 2 Caesar Problem

#include <stdlib.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

/*
 * Caesar.c
 * A program that encrypts messages using Caesar’s cipher. Your program must
 * accept a single command-line argument: a non-negative integer. If your program
 * is execute without any command-line arguments or with more than one command-line argument,
 * your program should tell the user and return a value of 1.
 *
 */

char rotate(char c, int n);

int main(int argc, string argv[])
{
    // Checks if argc 2 strings and argv second value is a digit
    if (argc == 2 && isdigit(*argv[1]))
    {
        // iterate through the argv
        for (int k = 0; k < strlen(argv[1]); k++)
        {
            // return 1 if there is non-digit value in argv
            if (!isdigit(argv[1][k]))
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }

        // turns argv into an integer value
        int i = atoi(argv[1]);

        // checks if that integer value us negative or zero
        if (i <= 0)
        {
            printf("Usage: ./caesar key\n");
            return 0;
        }
        else
        {
            // Get input text as plaintext
            string plaintext = get_string("plaintext: ");

            int n = strlen(plaintext);
            char ciphertext[n];

            // iterate through the plaintext and turn them into ciphertext
            printf("ciphertext: ");
            for (int j = 0; j < n; j++)
            {
                ciphertext[j] = rotate(plaintext[j], i);
                printf("%c", ciphertext[j]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}


// this function takes a character and integer and shifts the character by the amount of the integer
char rotate(char c, int n)
{
    char cipher;

    if (islower(c))
    {
        cipher = (((c + n) - 97) % 26) + 97;
    }
    else if (isupper(c))
    {
        cipher = (((c + n) - 65) % 26) + 65;
    }
    else
    {
        cipher = c;
    }

    return cipher;
}

You may also like to read: How much does the internet weigh?

(Visited 280 times, 7 visits today)