DEV Community

loading...
Cover image for Caesar Text Cipher Solution CS50 Problem set 2

Caesar Text Cipher Solution CS50 Problem set 2

cesareuseche profile image cesaruseche18 ・3 min read

On this week Problem set I had to make a text cipher solution made with C, this was a great challenge and it was very fun because you get used to working with arrays and ASCII values test a few solutions before finding the right solution to this problem.

If you are starting to Program with C and you are researching for a solution for this problem here I'm sharing how I made my solution that passed the CS50 check command.

Before starting remember that:

  • Every letter has an ASCII value
  • This is a great example to have a look before starting to code your solution. Cipher text example

First we start with a main function that it will take 2 arguments, first one takes number of arguments, and the second one takes an array of strings, like so.

int main(int argc, string argv[])
Enter fullscreen mode Exit fullscreen mode

Then, we need to checked if the user inputs less than or more than two arguments the user should be re-prompt. The second check we need to do if one of the arguments that the user inputs is alpha if it not alpha the user needs to e re-prompt again, we are going to loop through the string and we are going to check if the argument isalpha.

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./ceasar k");
        return 1;
    }

    // checking if one of the arguments isalpha (we need an integer) - Looping through the string of integers
    for (int key = 0; key < strlen(argv[1]); key++)
    {
        if (isalpha(argv[1][key]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
Enter fullscreen mode Exit fullscreen mode

Now, we are going to use a function called atoi, atoi it takes a string and transform that string into an integer. Then, we ask the user to input the plaintext and we need to loop through each letter of the plaintext, if it's not alpha we are going to print the current element of the array.

 // iterates over the plain text with a for loop
    for (int i = 0, length = strlen(plaintext); i < length; i++)
    {
        if (!isalpha(plaintext[i]))
        {
            //prints the current element of the array if it's not alpha
            printf("%c", plaintext[i]);
            continue;
        }
Enter fullscreen mode Exit fullscreen mode

Then, we are going to check if the current letter is uppercase and we calculate how far the current element is from lowercase "a" or uppercase "A".

Finally, we apply the formula that it was giving to us so we can know exactly the index of the ciphering of the letter that we want to cipher and we print the final result

 // index of the letter cyphering
        int ci = (pi + key) % 26;

        // printing the new character cyphered
        printf("%c", ci + offset);
    }


    printf("\n");
    return 0;
Enter fullscreen mode Exit fullscreen mode

I hope this is helpful and as always I'm leaving the whole solution down below if you want to have a look. Keep coding, Keep learning!

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


// main function takes 2 arguments, first one takes number of arguments, and the second one takes an array of strings
int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./ceasar k");
        return 1;
    }

    // checking if one of the arguments isalpha (we need an integer) - Looping through the string of integers
    for (int key = 0; key < strlen(argv[1]); key++)
    {
        if (isalpha(argv[1][key]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }

    int key = atoi(argv[1]) % 26; // converts the ASCII to an integer from "20" to 20 as an interger

    // takes the plaintext from the user
    string plaintext = get_string("plaintext: ");

    printf("ciphertext: ");

    // iterates over the plain text with a for loop
    for (int i = 0, length = strlen(plaintext); i < length; i++)
    {
        if (!isalpha(plaintext[i]))
        {
            //prints the current element of the array if it's not alpha
            printf("%c", plaintext[i]);
            continue;
        }
        // checking if the current element it's uppercase
        int offset = isupper(plaintext[i]) ? 65 : 97;
        // calculating how far the current element is from lowercase "a" or uppercase "A"
        int pi = plaintext[i] - offset;
        // index of the letter cyphering
        int ci = (pi + key) % 26;

        // printing the new character cyphered
        printf("%c", ci + offset);
    }


    printf("\n");
    return 0;


}
Enter fullscreen mode Exit fullscreen mode

Follow me on Github & Connect with me on LinkedIn

https://github.com/cesareuseche
https://www.linkedin.com/in/cesar-useche-profile/

Discussion (0)

Forem Open with the Forem app