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

int main(int argc, string argv[])
{
    // Verification
    if (argc != 2)
    {
        printf("Usage: ./ceaser <NON-NEG INTEGER>");
        return 1;
    }
    // Puts key into K
    int key = atoi(argv[1]);
    // Checks if interger is positive
    if (key < 0)
    {
        printf("Usage: ./ceaser <NON NEGATIVE INTEGER>");
        return 1;
    }

    // Prompts user for string to encrypt
    string code = get_string("Plaintext: ");

    for(int i = 0, n = strlen(code); i < n; i++)
    {
        // Checks if letter is upper or lower case
        if (isupper(code[i]))
            printf("%c",((code[i] - 65 + key) % 26) + 65);
        else if (islower(code[i]))
            printf( "%c",((code[i] - 97 + key) % 26) + 97);
        // Prints code if neither
        else
            printf("%c", code[i]);
    }
    printf("%s\n :ciphertext", code);
    return 0;
}

so when I try and run this say ./caesar 23 for barfoo it outputs:

yxocllbarfoo
:ciphertext

When it should be:

yxocll

Can anyone help me figure out what's wrong, I've been fiddling with this for days and cannot get it. Thank you!

1 Answer 1

0
yxocllbarfoo 

What is barfoo ?, It's what you print it in the last printf:

printf("%s\n :ciphertext", code);
1
  • Adding to MARS answer, you should go back and review the program spec on what the output should look like exactly. Getting it close isn't good enough.
    – Cliff B
    Nov 29, 2018 at 21:53

You must log in to answer this question.

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