#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
    //Check if there is 1 command-line argument, if not give the user an error
    if (argc != 2)
        printf("error please input a valid key\n");
        return 1;
        // convert key to an integer
        int key = atoi(argv[1]);
        //ask for plaintext
        string plaintxt = get_string();
        for (int i = 0, j = strlen(plaintxt); i < j; i++)
            //if the current letter is uppercase run this segment
            if (isupper(plaintxt[i]))
                printf("%c", (((plaintxt[i] + key) - 65) % 26) + 65);
            //If lowercase run this segment
            else if (islower(plaintxt[i]))
                printf("%c", (((plaintxt[i] + key) - 97) % 26) + 97);
                printf("%c", plaintxt[i]);
        return 0;

When I run check50 I get the following--

:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
    expected "ciphertext: b\n", not "ciphertext:\nb"
:( encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yxo...", not "ciphertext:\nyx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected "ciphertext: EDU...", not "ciphertext:\nED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected "ciphertext: FeV...", not "ciphertext:\nFe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
    expected "ciphertext: one...", not "ciphertext:\non..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    expected "ciphertext: iad...", not "ciphertext:\nia..."
:) handles lack of argv[1]

I have tried adding \n in the line: printf("ciphertext:"); e.g. printf("ciphertext:\n"); but this does not work. Now I am stuck.

Thanks guys!


Have you tried your programme? After finishing the programme, your prompt is on the same line as the output, but should be on the next line.

Add a printf (or equivalent) printing just a newline \n, place it after your for loop. but before you return.

You must log in to answer this question.

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