Guest User

Untitled

a guest
May 22nd, 2020
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. void convertToUpperCase();
  7. bool validateKey();
  8. bool verifyKeyOnlyContainsLetters();
  9. bool checkIfKeyContainsAllUniqueCharacters();
  10. string convertToCipherText(string plainttext);
  11.  
  12. string key;
  13.  
  14. int main(int argc, string argv[])
  15. {
  16.     //Checks if no key has been provided.
  17.     if (argv[1] == NULL)
  18.     {
  19.         printf("You must provide a 26 digit alphabetic key containing each letter once and only once.\n");
  20.         printf("e.g VCHPRZGJNTLSKFBDQWAXEUYMOI\n");
  21.         return 1;
  22.     }
  23.  
  24.     //Assigns the key from command line to the global variable 'key' and converts it all to uppercase.
  25.     key = argv[1];
  26.     convertToUpperCase(key);
  27.  
  28.     //Checks if function validateKey returns false. See line for function.
  29.     if (!validateKey())
  30.     {
  31.         return 1;
  32.     }
  33.  
  34.     string plaintext = get_string("plaintext: ");
  35.     printf("ciphertext: %s\n", convertToCipherText(plaintext)); //See line 119 for function.
  36.     //Returns 0 to exit program successfully.
  37.     return 0;
  38. }
  39.  
  40. //Function to simply loop over every character in the key and convert them to uppercase.
  41. void convertToUpperCase()
  42. {
  43.     for (int i = 0; i < strlen(key); i++)
  44.     {
  45.         key[i] = toupper(key[i]);
  46.     }
  47. }
  48.  
  49. //Performs a couple of checks by function calls to verify key is valid as per the specification given.
  50. bool validateKey()
  51. {
  52.     bool result = false;
  53.     if (!verifyKeyOnlyContainsLetters()) //See line 75.
  54.     {
  55.         printf("Key must only contain letters.\n");
  56.         return result;
  57.     }
  58.     if (strlen(key) != 26) //Makes sure the key is exactly 26 characters long.
  59.     {
  60.         printf("Key must contain exactly 26 characters.\n");
  61.         return result;
  62.     }
  63.  
  64.     if (!checkIfKeyContainsAllUniqueCharacters()) //See line 91.
  65.     {
  66.         printf("Key must contain each character once and only once.\n");
  67.         return result;
  68.     }
  69.  
  70.     //If all the tests pass, result is assigned true and returned.
  71.     result = true;
  72.     return result;
  73. }
  74.  
  75. bool verifyKeyOnlyContainsLetters()
  76. {
  77.     bool result = true;
  78.  
  79.     //Loops through every character of the key and checks that they're all between the range of the uppercase alphabet.
  80.     //(key was converted to uppercase earlier to avoid checking the lowercase letters.)
  81.     for (int i = 0; i < strlen(key); i++)
  82.     {
  83.         if (!(key[i] >= 'A' && key[i] <= 'Z'))
  84.         {
  85.             result = false;
  86.         }
  87.     }
  88.     return result;
  89. }
  90.  
  91. bool checkIfKeyContainsAllUniqueCharacters()
  92. {
  93.     bool result = false;
  94.     int containsLetter = 0;
  95.  
  96.     //Function loops through the alphabet checking each character of the key as it goes.
  97.     for (char currentChar = 'A'; currentChar <= 'Z'; currentChar++)
  98.     {
  99.         for (int i = 0; i <= strlen(key); i++)
  100.         {
  101.             //If the key contains the current letter, it incriments the counter variable (containsLetter) and then moves on to the next letter.
  102.             if (currentChar == key[i])
  103.             {
  104.                 containsLetter++;
  105.                 //Break statement is essential as once it see's a character in the key it stops and moves on to the next alphabetic character, this checks for duplicate letters within the key.
  106.                 break;
  107.             }
  108.         }
  109.     }
  110.  
  111.     //If the counter was successfully incremented to 26 i.e it contains each letter once and only once, result is then assigned true and returned.
  112.     if (containsLetter == 26)
  113.     {
  114.         result = true;
  115.     }
  116.     return result;
  117. }
  118.  
  119. string convertToCipherText(string plaintext)
  120. {
  121.     string ciphertext = plaintext;
  122.  
  123.     for (int i = 0; i < strlen(ciphertext); i++)
  124.     {
  125.         //If block to ignore punctuation.
  126.         if (plaintext[i] == ' ' || plaintext[i] == ',' || plaintext[i] == '.' || plaintext[i] == '?')
  127.         {
  128.             //DO NOTHING
  129.         }
  130.         //Checks if the plaitext is a capital letter, if so it takes off 97 (see asciichart.com) to get the index to 0 and assigns the corresponding key value to the ciphertext.
  131.         if (plaintext[i] >= 65 && plaintext[i] <= 90)
  132.         {
  133.             ciphertext[i] = key[plaintext[i] - 65];
  134.         }
  135.         //Same as previous step but for lowercase letters. Takes off 97 this time and then converts the assigned character to lowercase as the key is all uppercase characters.
  136.         if (plaintext[i] >= 97 && plaintext[i] <= 122)
  137.         {
  138.             ciphertext[i] = key[plaintext[i] - 97];
  139.             ciphertext[i] = tolower(ciphertext[i]);
  140.         }
  141.     }
  142.  
  143.     return ciphertext;
  144. }
Add Comment
Please, Sign In to add comment