Home > Mobile >  CS50 Week 2 Caesar Practice
CS50 Week 2 Caesar Practice

Time:12-06

My code seems to be working properly except at the point when it should print the final output. The problem is to input a string and output an encrypted version. The encryption works by adding an int defined as the key and then adding that value to each character of the ascii values of the inputed string. My issue is that when the cypher text is outputted there are only spaces and no letters or even numbers.

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

int main(int argc, string argv[]) {

    int key = atoi(argv[1]);

    printf("%i\n", key);

    if (argc != 2) {
        printf("Usage: ./ceasar key\n");
    } else {
        string text = get_string("Plaintext: ");

        for (int i = 0, len = strlen(text); i < len; i  ) {
            int cipher = text[i];
            int ciphertext = cipher   key;
            int ciphermod = ciphertext % 26;
            printf("%c", ciphermod);
        }
        printf("\n");
    }
}

CodePudding user response:

The formula to compute the ciphered characters is incorrect:

  • you should only encode letters
  • you should subtract the code for the first letter 'a' or 'A'
  • you should add the code for the first letter 'a' or 'A' to the encoded index.

Note also that you should not use argv[1] until you have checked that enough arguments have been passed. Here is a modified version:

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

int main(int argc, string argv[]) {
    if (argc != 2) {
        printf("Usage: ./ceasar key\n");
    } else {
        int key = atoi(argv[1]);

        printf("%i\n", key);

        string text = get_string("Plaintext: ");

        for (int i = 0, len = strlen(text); i < len; i  ) {
            int c = text[i];
            if (c >= 'a' && c <= 'z') {
                int cipher = c - 'a';
                int ciphertext = cipher   key;
                int ciphermod = ciphertext % 26;
                c = 'a'   ciphermod;
            } else
            if (c >= 'A' && c <= 'Z') {
                int cipher = c - 'A';
                int ciphertext = cipher   key;
                int ciphermod = ciphertext % 26;
                c = 'A'   ciphermod;
            }
            printf("%c", c);
        }
        printf("\n");
    }
    return 0;
}

CodePudding user response:

You've got a few issues going on here. Please make sure to thoroughly read the assignment before turning to others for assistance.

The assignment requires you to:

  • Only encode alphabetic characters. Look to the function isalpha() for this.
  • Encode both uppercase and lowercase characters accurately. Note that, in ASCII, uppercase letters and lowercase letters are separate entities.
    • Meaning, you must have your code be able to handle both, as they are each handled differently.
    • Perhaps taking some time to sit and take in the ASCII table may be helpful to you, as it will help you understand what is really happening when you add the key.
  • Use the correct formula for encoding letters. The i'th ciphered letter ci corresponding to the i'th plaintext letter pi is defined as ci = (pi k) % 26.
    • Your code is equivalent to this formula, but it does not account for wrapping, uppercase/lowercase letters, etc. The project specification doesn't just ask you to repeat the formula, it asks you to solve a problem using it. To do so, you must understand it. I explain more, subsequently.

I recommend:

  • Modifying the text in-place. Currently, you calculate the ciphered text and print it. If you add code for modifying the text where it sits, it'll make ignoring non-alphabetic characters easier.
  • Modify the formula.
    • Where
  • Related