Advertisement
Guest User

Caesar

a guest
Feb 19th, 2019
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. int main(int argc, string argv[])
  7. {
  8.     int KEY = 0;
  9.     bool NUM = true;
  10.     int j = 0;
  11.     //checks if there are 2 args entered
  12.     if (argc == 2)
  13.     {
  14.         j = strlen(argv[1]);
  15.     }
  16.     else
  17.     {
  18.         //explains useage if argv1 was not correctly entered
  19.         printf("Usage: ./cesar key\n");
  20.         return 1;
  21.     }
  22.     //checks if argv[1] is a number
  23.     for (int i = 0; i < j; i++)
  24.     {
  25.         if (isdigit(argv[1][i]))
  26.         {
  27.             NUM = true;
  28.         }
  29.         else
  30.         {
  31.             NUM = false;
  32.             break;
  33.         }
  34.     }
  35.     //only runs main program if all checks above are true (argv[1] is a number and there is only 2 args entered)
  36.     if (NUM == true)
  37.     {
  38.         string plain = (get_string("Enter your plaintext: \n"));
  39.         //converts argv[1] to int
  40.         for (int i = 0, n = strlen(argv[1]); i < n; i++)
  41.         {
  42.             KEY = KEY * 10 + (int) argv[1][i] - '0';
  43.         }
  44.         //reduces the key smallest necessary integer. Over 26 loops around extra.
  45.         while (KEY > 26)
  46.         {
  47.             KEY = KEY - 26;
  48.         }
  49.         //iterates over the length of the plaintext string    
  50.         for (int i = 0, n = strlen(plain); i < n; i++)
  51.         {
  52.             //checks if the character is uppercase
  53.             if (plain[i] >= 'A' && plain[i] <= 'Z')
  54.             {
  55.                 //checks that the character plain[i] + the key remains within the uppercase letters in ASCII
  56.                 char c = plain[i] + KEY;
  57.                 if (c >= 'A' && c <= 'Z')
  58.                 {
  59.                     printf("%c", c);
  60.                 }
  61.                 //if plain[i] + key is higher than 'Z' it will remove '26' to bring it back within confines of A through Z
  62.                 else
  63.                 {
  64.                     c = c - 26;
  65.                     printf("%c", c);
  66.                 }
  67.             }
  68.             //checks if the character plain[i] is a lowercase letter
  69.             else if (plain[i] >= 'a' && plain[i] <= 'z')
  70.             {
  71.                 //checks that the character plain[i] + the key remains within the lowercase letters in ASCII
  72.                 char c = plain[i] + KEY;
  73.                 if (c >= 'a' && c <= 'z')
  74.                 {
  75.                     printf("%c", c);
  76.                 }
  77.                 else
  78.                 {
  79.                     //if plain[i] + key is higher than 'z' it will remove '26' to bring it back within confines of a through z
  80.                     c = c - 26;
  81.                     printf("%c", c);
  82.                 }
  83.             }
  84.             //if character is not a letter, just prints the character as is
  85.             else
  86.             {
  87.                 printf("%c", plain[i]);
  88.             }
  89.         }
  90.         //writes a new line for cleanliness at the end
  91.         printf("\n");
  92.         return 0;
  93.     }
  94.     else
  95.     {
  96.         //explains useage if argv1 was not correctly entered
  97.         printf("Usage: ./cesar key\n");
  98.         return 1;
  99.     }
  100. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement