Soleyu

Untitled

Mar 31st, 2022
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1.  
  2. #include <cs50.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7.  
  8.  
  9. string substitutioncipher(string plaintext, string key);
  10.  
  11. int main(int argc, string argv[])
  12. {
  13.      //First we check there are only 2
  14.     if (argc != 2)
  15.     {
  16.         printf("./substitution key\n");
  17.         return 1;
  18.     }
  19.  
  20.     //we check that there are exactly 26 letters
  21.     int arglen = strlen(argv[1]);
  22.     if (arglen != 26)
  23.     {
  24.         printf("Key must contain 26 characters.\n");
  25.         return 1;
  26.     }
  27.  
  28.     //we check they are only leters and get the key and uppercase it
  29.     string key = argv[1];
  30.     for (int i = 0; i < arglen; i++)
  31.     {
  32.         if (!isalpha(key[i]))
  33.         {
  34.             printf("The key must only contain letters\n");
  35.             return 1;
  36.         }
  37.         key[i] = toupper(key[i]);
  38.     }
  39.  
  40.     //now we check for duplicate characters
  41.     //Basically we do a double loop checking if any character is equal to another
  42.     //We also put a not equal there so characters in the same spot dont trigger the check
  43.     for (int i = 0; i < 26; i++)
  44.     {
  45.         for (int j = 0; j < 26; j++)
  46.         {
  47.             if (key[i] == key[j] && i != j)
  48.             {
  49.                 printf("No duplicate characters allowed in key\n");
  50.                 return 1;
  51.             }
  52.         }
  53.     }
  54.     string plaintext = get_string("plaintext: ");
  55.     string ciphertext = substitutioncipher(plaintext, key);
  56.     int plenght = strlen(ciphertext);
  57.     /*
  58.     char teststring[plenght];
  59.     strcpy(teststring, ciphertext);
  60.     string ciphertexttest = teststring;
  61.     ciphertexttest[plenght] = '\0';
  62.     */
  63.  
  64.     ciphertext[plenght] = '\0';
  65.     printf("ciphertext: %s\n", ciphertext);
  66.     //printf("ciphertexttest: %s\n", ciphertexttest);
  67.     return 0;
  68. }
  69.  
  70. string substitutioncipher(string plaintext, string key)
  71. {
  72.     int lenght = strlen(plaintext);
  73.     char cyphertextArr[lenght];
  74.     cyphertextArr[lenght] = '\0';
  75.     string cyphertext = cyphertextArr;
  76.  
  77.     for (int i = 0; i < lenght; i++)
  78.     {
  79.         if (isupper(plaintext[i]))
  80.         {
  81.             int cur_letter = plaintext[i] - 'A';
  82.             char cyp_letter = key[cur_letter];
  83.             cyphertext[i] = toupper(cyp_letter);
  84.         }
  85.         else if (islower(plaintext[i]))
  86.         {
  87.             int cur_letter = plaintext[i] - 'a';
  88.             char cyp_letter = key[cur_letter];
  89.             cyphertext[i] = tolower(cyp_letter);
  90.         }
  91.         else
  92.         {
  93.             cyphertext[i] = plaintext[i];
  94.         }
  95.     }
  96.  
  97.     return cyphertext;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment