Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int main(int argc, string argv[])
- {
- if (argc != 2)
- {
- printf("USAGE: ./caesar NUM\n");
- return 1;
- }
- int offset=0;
- offset = atoi(argv[1]);
- offset = offset % 26; //remove redundant offset multiples of 26
- //printf("The shift is %i\n", offset);
- string plaintext;
- plaintext = get_string("plaintext: ");
- int shift=0;
- char newLetter;
- printf("ciphertext: ");
- //process each letter.
- for (int i=0; i<strlen(plaintext); i++)
- {
- if (isupper(plaintext[i]) != 0)
- {
- shift = 64;
- plaintext[i] = plaintext[i] - shift; //scale letters back to to 1-26
- plaintext[i] = plaintext[i] + offset; //add offset cipher
- plaintext[i] = plaintext[i] % 26; //adjust if over 26
- plaintext[i] = plaintext[i] + shift; //scale letters back to to original range
- printf("%c", plaintext[i]);
- }
- else if (islower(plaintext[i]) != 0)
- {
- shift = 96;
- plaintext[i] = plaintext[i] - shift; //scale letters back to to 1-26
- plaintext[i] = plaintext[i] + offset; //add offset cipher
- plaintext[i] = plaintext[i] % 26; //adjust if over 26
- plaintext[i] = plaintext[i] + shift; //scale letters back to to original range
- printf("%c", plaintext[i]);
- }
- else
- {
- printf("%c", plaintext[i]);
- }
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement