Anonymous Anonymous - 11 days ago
134 0

No description

C

caesar flawed

#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]);

    //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("Enter 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]-65) + s%26;
                printf("%c", l+65);
            }

            if (islower(p[i])) {

                char m = (p[i]-97) + s%26;
                printf("%c", m+97);
            }

        }

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

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

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

       }
    }

    ///print new line after ciphertext///

    printf("\n");

    printf("argv1 is %i\n", s);

}