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

    int main (int argc, string argv[])
    {   
        //checks if argc is 2
        if (argc != 2)
        {
            return 1;
        }

        int k = atoi(argv[1]) % 26;

        string p = GetString();

        //reiterates each letter
        for (int i = 0, n = strlen(p); i < n ; i++)
        {
            if (isalpha(p[i]), isupper(p[i]))
            {
                int letter = p[i] - 65;
                int result = ((letter + k) % 26) + 65;
                printf("%c", result);
            }   
            else if (isalpha(p[i]), islower(p[i]))
            {
                int letter = p[i] - 97;
                int result = ((letter + k) % 26) + 97;
                printf("%c", result);
            }
            else
            {
                printf("%c", p[i]);
            }
        }
        return 0;
    }



    :) caesar.c exists
    :) caesar.c compiles
    :( encrypts "a" as "b" using 1 as key
       \ expected output, but not "b"
    :( encrypts "barfoo" as "yxocll" using 23 as key
       \ expected output, but not "yxocll"
    :( encrypts "BARFOO" as "EDUIRR" using 3 as key
       \ expected output, but not "EDUIRR"
    :( encrypts "BaRFoo" as "FeVJss" using 4 as key
       \ expected output, but not "FeVJss"
    :( encrypts "barfoo" as "onesbb" using 65 as key
       \ expected output, but not "onesbb"
    :( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
       \ expected output, but not "iadxp, emk tqxxa!"
    :( handles lack of argv[1]
       \ expected output, not an exit code of 1
    https://sandbox.cs50.net/checks/853f5d3a161e4c0fab496d22591d2c07
1
  • Very nice code by the way! I've never seen booleans separated by commas in C. I'll add that to my toolbox. It looks so clean. Commented Oct 20, 2016 at 13:25

1 Answer 1

1

check50 is really picky right?

You are missing a new line at the end of your encrypted text.

Just add something like

printf("\n");

before your return 0;

1
  • Also, the program should complain before returning 1 (output some reminder on how to use it for example)
    – Blauelf
    Commented Oct 20, 2016 at 13:26

You must log in to answer this question.

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