Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- string get_input()
- {
- string plain = get_string("plaintext: ");
- return plain;
- }
- int caesar_cipher(string s, int k)
- {
- printf("ciphertext: ");
- string cipher;
- int SIZE = strlen(s);
- for (int i = 0; i < SIZE; i++)
- {
- // Creates a place to store the ciphered value
- int c_key;
- if ( isupper(s[i]) )
- {
- c_key = ((int) s[i] - 'A' + k) % 26 + (int) 'A';
- } else if ( islower(s[i]) )
- {
- c_key = ((int) s[i] - 'a' + k) % 26 + (int) 'a';
- } else {
- c_key = (int) s[i];
- }
- // Prints the (char) -> int s[i] + 1;
- printf("%c", (char) c_key);
- }
- printf("\n");
- return 0;
- }
- int main(int argc, string argv[])
- {
- if ((argc > 1 && argc < 3))
- {
- // Stores key
- int k = atoi(argv[1]);
- // Gets plaintext
- string plain = get_input();
- // Cracks it up
- caesar_cipher(plain, k);
- } else
- {
- printf("Usage: ./caesar key\n");
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement