Skip to main content

Question on Caesar Pset2

I am getting an error code

:( encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yxo...", not "ciphertext:ycip..."

I am trying to add the ciphertext: tp the printf("%c" .....) but it is prinitng with each letter this is my code

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

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

    // it must have 2 argv
    if (argc != 2)
    {
        printf("Failed\n");
        return 1;
    }

    // now lets add a key value
    int key = atoi(argv[1]);

    if (key < 0)
    {
        printf("Failed\n");
        return 1;
    }

    else
    {
        // get the code
        printf("plaintext:");
        string p = get_string();

        for (int i = 0, n = strlen(p); i < n; i++)
        {
            if islower(p[i])
                printf("%c", (((p[i] + key) - 97) % 26) + 97);
            else if isupper(p[i])
                printf("%c", (((p[i] + key)  - 65) % 26) + 65);
            else
                printf("%c", p[i]);
        }
        printf("\n");
        return 0;
    }
}