Advertisement
Guest User

Untitled

a guest
May 22nd, 2020
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. //for character classification functions
  5. #include <ctype.h>
  6.  
  7. //function declaration
  8. bool validate(string key);
  9.  
  10. //global variable
  11. string abc = "abcdefghijklmnopqrstuvwxyz";
  12.  
  13. int main(int argc, string argv[])
  14. {
  15.     string key = NULL, plaintext = NULL, ciphertext = NULL; //Create variable outside if statement. Otherwise if the statement isn't true the variable isn't created for later use.
  16.     int length; //Generally good practice to create all your variables at the top and only create variables that only need to be used once within your loops.
  17.  
  18.     if (argc == 2)
  19.     {
  20.         key = toupper(argv[1]); //Keeping the key as uppercase.
  21.     }
  22.     else
  23.     {
  24.         printf("Usage: ./substitution key \n");
  25.         return 1;
  26.     }
  27.  
  28.     if (!validate(key)) //No need to write == true, just putting a function in brackets like (validate(key)) is checking if it returns true, the ! checks if it's false, just a small change to help with readability.
  29.     {
  30.         printf("Key must contain 26 unique characters\n");
  31.         return 1;
  32.     }
  33.  
  34.     plaintext = get_string("plaintext: ");
  35.     length = strlen(plaintext);
  36.  
  37.     for(int i = 0; i < length; i++)
  38.     {
  39.         if (isupper(plaintext[i]) != 0)
  40.         {
  41.             for (int j = 0; j < 26; j++ )
  42.             {
  43.                 if (abc[j] == tolower(plaintext[i])) //Problem is how you're handling copying your arrays.
  44.                 {
  45.                     ciphertext[i] = toupper(key[j]);
  46.                     break;
  47.                 }
  48.             }
  49.         }
  50.         else if (islower(plaintext[i]) != 0)
  51.         {
  52.             for (int j = 0; j < 26; j++ )
  53.             {
  54.                 if (abc[j] == plaintext[i])
  55.                 {
  56.                     ciphertext[i] = tolower(key[j]);
  57.                     break;
  58.                 }
  59.             }
  60.         }
  61.         else
  62.         {
  63.             ciphertext[i] = plaintext[i];
  64.         }
  65.     }
  66.     printf("ciphertext: %s\n", ciphertext);
  67.     return 0;
  68. }
  69.  
  70. bool validate(string key)
  71. {
  72.     int match_counter = 0;
  73.     if (strlen(key) == 26)
  74.     {
  75.         for (int i = 0; i < 26; i++)
  76.         {
  77.             for (int j = 0; j < 26; j++)
  78.             {
  79.                 if(tolower(key[j]) == abc[i])
  80.                 {
  81.                     match_counter++;
  82.                     break;
  83.                 }
  84.             }
  85.         }
  86.  
  87.         if(match_counter == 26)
  88.         {
  89.             return true;
  90.         }
  91.         else
  92.         {
  93.             return false;
  94.         }
  95.     }
  96.     else
  97.     {
  98.         return false;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement