Anonymous Anonymous - 11 days ago
148 0

No description

C

caesar attempt 2

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

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

    ///turn command line string into int///

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

    //return error message for no command line arg///

    if (argc!=2) {

        printf("Error: No command line argument\n");
        return 1;
    }

    //prompt user for plaintext//

        printf("plaintext: ");
        string p = get_string();

    //print loop for ciphertext//

    printf("ciphertext: ");

    int n = strlen(p);

    for (int i=0; i<n; i++) {

    //if char is alphabetical inner loops preserve case//

        if (isalpha(p[i])) {

            if (isupper(p[i])) {

                char l = p[i]+ s;

                if (l>=90) {

                    l = s + 65;
                }

                printf("%c", l);
            }

            if (islower(p[i])) {

                char m = p[i] + s;

                if (m>=122) {

                    m = s + 97;
                }

                printf("%c", m);
            }

        }

    ///if not a letter, prints as is//

       if (isalpha(p[i]) == false) {

        printf("%c", p[i]);

       }
    }

    ///print new line after ciphertext///

    printf("\n");
    return 0;

}


:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:( encrypts "barfoo" as "yxocll" using 23 as key
    output not valid ASCII text
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:( encrypts "barfoo" as "onesbb" using 65 as key
    expected "ciphertext: one...", not "ciphertext: onn..."
:( handles lack of argv[1]
    failed to execute program due to segmentation fault