Guest User

sub.c

a guest
Feb 4th, 2020
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.91 KB | None | 0 0
  1. // This program allows the user to implement a
  2. // substitution cipher. That is, at runtime, the
  3. // user enters a substitution key, and a string
  4. // of plaintext to convert to ciphertext.
  5.  
  6. // includes
  7. #include <stdio.h>
  8. #include <cs50.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12. // function prototypes
  13. int validateKey(int args, string argument);
  14. string encrypt(string key, string plain);
  15.  
  16.  
  17. // MAIN FUNCTION
  18.  
  19. int main(int argc, string argv[])
  20. {
  21.  
  22.     string keyInput = argv[1];                      // user's key input
  23.     int errorCode = validateKey(argc, argv[1]);     // key validation
  24.  
  25.     // if the error code is 1, throw an exception, and exit
  26.     if (errorCode == 1)
  27.     {
  28.         return errorCode;
  29.     }
  30.  
  31.     // get some plain text from the user
  32.     string plainText = get_string("plaintext: ");
  33.     // printf("\n");
  34.  
  35.     // store encrypted output in a string
  36.     string encrypted = encrypt(keyInput, plainText);
  37.  
  38.     // print encrypted text
  39.     printf("ciphertext: %s\n", encrypted);
  40.  
  41.     // end program
  42.     return 0;
  43. }
  44.  
  45.  
  46. // ENCRYPTOR
  47. string encrypt(string key, string plain)
  48. {
  49.     char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";  // alphabet reference
  50.  
  51.     // iterate through the plaintext
  52.     for (int i = 0, len = strlen(plain); i < len; i++)
  53.     {
  54.         bool alphabetic = false;    // set up a variable to record if current char is alphabetic
  55.  
  56.         // iterate through the alphabet
  57.         for (int j = 0, length = strlen(alphabet); j < length; j++)
  58.         {
  59.             // check if current char is lower-case letter
  60.             if (plain[i] == alphabet[j])
  61.             {
  62.                 // if the key is in upper-case, convert to lower-case
  63.                 if (isupper(key[j]))
  64.                 {
  65.                     plain[i] = tolower(key[j]);
  66.                 }
  67.                 else
  68.                 {
  69.                     plain[i] = key[j];
  70.                 }
  71.                 alphabetic = true;      // record that it's an alphabetic char
  72.                 j = strlen(alphabet);   // skip the rest of the alphabet scan
  73.             }
  74.             else if (plain[i] == toupper(alphabet[j]))
  75.             {
  76.                 plain[i] = toupper(key[j]);
  77.                 alphabetic = true;              // record that it's an alphabetic char
  78.                 j = strlen(alphabet);           // skip the rest of the alphabet scan
  79.             }
  80.         }
  81.  
  82.         // if not alphabetic
  83.         if (alphabetic == false)
  84.         {
  85.             plain[i] = plain[i];
  86.         }
  87.     }
  88.  
  89.     // returned the ciphertext, now stored in the original plain variable
  90.     return plain;
  91. }
  92.  
  93.  
  94. // KEY VALIDATOR
  95. int validateKey(int args, string key)
  96. {
  97.     // if not enough arguments
  98.     if (args < 2)
  99.     {
  100.         printf("Try typing in a cipher key after ./substitution.\n");
  101.         return 1;
  102.     }
  103.     // if too many arguments
  104.     else if (args > 2)
  105.     {
  106.         printf("Try typing only one argument after ./substitution.\n");
  107.         return 1;
  108.     }
  109.     // if right number of arguments
  110.     else
  111.     {
  112.         int length = strlen(key);
  113.  
  114.         // but wrong length
  115.         if (length < 26 || length > 26)
  116.         {
  117.             printf("Try using *exactly 26* upper-case or lower-case letters in your key.\n");
  118.             return 1;
  119.         }
  120.  
  121.         // if right length
  122.         else
  123.         {
  124.             // iterate through the key
  125.             for (int i = 0; i < length; i++)
  126.             {
  127.                 // if it contains non-alphabetic characters
  128.                 if ((key[i] < 65 || key[i] > 90) && (key[i] < 97 || key[i] > 122))
  129.                 {
  130.                     printf("Try using exactly 26 upper-case or lower-case LETTERS in your key.\n");
  131.                     return 1;
  132.                 }
  133.             }
  134.  
  135.             // declare a variable to keep track of the current character
  136.             char currentChar;
  137.  
  138.             // iterate through the key
  139.             for (int i = 0; i < length; i++)
  140.             {
  141.                 // set currentChar value to currently selected character in key
  142.                 currentChar = key[i];
  143.                 // initialize a counter variable
  144.                 int counter = 0;
  145.  
  146.                 // scan through the key
  147.                 for (int j = 0; j < length; j++)
  148.                 {
  149.                     // if the currently selected character matches any in the key, increment counter
  150.                     if (currentChar == key[j])
  151.                     {
  152.                         counter++;
  153.                     }
  154.  
  155.                     // if the counter is greater than 1, return an error with a console warning
  156.                     if (counter > 1)
  157.                     {
  158.                         printf("Try not repeating any letters in the key.\n");
  159.                         return 1;
  160.                     }
  161.                 }
  162.             }
  163.  
  164.             // if the key is valid by all the above criteria
  165.             return 0;
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment