0

If someone has the time to explain to me how I can fix my code that would be greatly appreciated! Thanks :)

Problems that I need help with:

1 - Program needs to accept single character inputs like 'a' and output a single ciphered character (not followed by random symbols)

2 - Program needs to accept string with multiple punctuations like 'world, say hello!; and output a ciphered string (not followed by random symbols)

Here is check50's feedback:

:) caesar.c exists.

:) caesar.c compiles.

:( encrypts "a" as "b" using 1 as key output not valid ASCII text

:) encrypts "barfoo" as "yxocll" using 23 as key

:) encrypts "BARFOO" as "EDUIRR" using 3 as key

:) encrypts "BaRFoo" as "FeVJss" using 4 as key

:) encrypts "barfoo" as "onesbb" using 65 as key

:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key output not valid ASCII text

:) handles lack of argv[1]

:) handles non-numeric key

:) handles too many arguments

Here is my code:

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

int main(int argc, string argv[])
{
    // Declare only 2 arguments because argc[0] is name of problem, so argc[1] is input of human
    if (argc != 2)
    {
        printf("Usage: ./caesar Please enter a cipher key\n"); //Provide this 'error message' if the human does not enter a cipher key
        return 1;
    }

    // Check if input by human is a digit (assuming only positive digit)
    string string_key = argv[1];
    for (int i = 0, n = strlen(string_key); i < n; i++)
    {
        if (isdigit(string_key[i]) == false)
        {
            printf("Usage: ./caesar Please enter an integer for the cipher key\n"); //Provide this 'error message' if the human entered a cipher key that isn't a digit
            return 1;
        }
    }

    // If input by human is a digit, then CONVERT the plain text string_key to an int
    int key = atoi(string_key);

    // Prompt human to entere a plaintext for a string to encipher
    string plaintext = get_string("plaintext: ");

    //Declare variables to determine upper and lower case differences
    int uppercase = 65;
    int lowercase = 97;

    // Declare variable that gets length of plaintext
    int text_length = strlen(plaintext);

    // Create the ciphertext array with the length of the plaintext
    char ciphertext[text_length];

    // For loop to check plaintext
    char charactercheck;
    for (int i = 0; i < text_length; i++)
    {
        charactercheck = plaintext[i];
        if (isalpha(charactercheck)) // this checks if the character is a letter
        {
            if (isupper(charactercheck)) // this checks if the character is uppercase
            {
                charactercheck = charactercheck - uppercase; //convert to alpha values
                charactercheck = (charactercheck + key) % 26; //shift to key
                charactercheck = charactercheck + uppercase; //convert back to ASCII values
                ciphertext[i] = charactercheck; //add to ciphertext
            }
            else
            {
                charactercheck = charactercheck - lowercase; //convert to alpha values
                charactercheck = (charactercheck + key) % 26; //shift to key
                charactercheck = charactercheck + lowercase; //convert back to ASCII values
                ciphertext[i] = charactercheck; //add to ciphertext
            }
        }
        else
        {
            ciphertext[i] = charactercheck; //Add to ciphertext
        }
    }
    printf("ciphertext: %s\n", ciphertext); //Output the ciperhered text

}

1 Answer 1

1

The code encrypts the chars from plaintext and stores them in the ciphertext array. But, where is the end of string marker in ciphertext[]?

Funny thing about printf() and related functions. They depend on the presence of the end of string marker, "\0", to work correctly. If you forget to add it, the printf() function call will continue printing whatever random data follows in memory until it detects the EOS marker, aka, a zero byte. That's why it's printing "random" characters.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • You're an absolute star. Thank you so much. This has answered my question and helped me better understand. THANK YOU!
    – karlares
    Sep 15, 2022 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .