Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- //
- int main(int argc, string argv [])
- {
- // Check to make sure argc is not greater than 2
- if (argc != 2)
- {
- printf("Usage: ./caesar key\n");
- return 1;
- }
- // Iterate through the charcherts in agrv and make sure they are all digits, if they are not print an error message and return 1
- for (int k = 0, n = strlen(argv[1]); k < n; k++)
- if (isdigit(argv[1][k]) == 0)
- {
- printf("Usage: ./caesar key \n");
- return 1;
- }
- // Turn the string into int's
- int ato = atoi(argv[1]);
- //check to make sure the key is not greater than 26
- if (ato > 26)
- {
- printf("Usage: ./caesar key \n");
- return 1;
- }
- // Get a string from the user and store in the variable called plaintext, and then print ciphertext
- string plaintext = get_string("plaintext: ");
- printf("ciphertext: ");
- // Iterate over in plain1 and if plain1 is less than the strlen of plaintext, print the character that plain1 will be at and add 1 to that character and also add one to plain1, print a newline and exit by returning 0. That is if plain1 = 0, the 0th character in plaintext[plain1] will be h, if hello is enterted for plaintext.
- for (int p1 = 0; p1 < strlen(plaintext); p1++)
- if (p1 >= 65 && p1 = < 90)
- if (plaintext[p1] + ato > 90)
- {
- printf("%c", plaintext[p1]);
- }
- else
- {
- printf("%c", plaintext[p1] + ato);
- }
- else if (p1 >= 97 && p1 =< 122)
- if (plaintext[p1] + ato > 122)
- {
- printf("%c", plaintext[p1]);
- }
- else
- {
- printf("%c", plaintext[p1] + ato);
- }
- else printf("%c", plaintext[p1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement