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>
- int main(int argc, string argv[])
- {
- char *key = argv[1];
- char n;
- int newkey;
- //rejects too many arguments
- if (argc != 2)
- {
- printf("./caesar key\n");
- return 1;
- }
- // converts char key into int newkey
- for (int i = 0; n = strlen(key), i < n; i++)
- {
- if isdigit(key[i])
- {
- newkey = atoi(key);
- }
- // rejects letters
- else
- {
- printf("./caesar key\n");
- return 1;
- }
- }
- // gets plaintext from user
- char *plain = get_string("plaintext: ");
- // prints modded cipher text
- printf("ciphertext: ");
- for (int i = 0; n = strlen(plain), i < n; i++)
- {
- // checks case
- if (isupper(plain[i]))
- {
- int plainupper = (plain[i] - 65);
- int ciphupper = (plainupper + newkey) % 26;
- char fintext = (ciphupper + 65);
- printf("%c", fintext);
- }
- // checks case
- if (islower(plain[i]))
- {
- int plainlower = (plain[i] - 97);
- int ciphlower = (plainlower + newkey) % 26;
- char finntext = (ciphlower + 97);
- printf("%c", finntext);
- }
- else
- // prints whatever was typed if it isnt upper or lower case
- {
- printf("%c", plain[i]);
- }
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement