Advertisement
Guest User

Untitled

a guest
Feb 20th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. // #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. string encipher(string plaintext, string cipher);
  8.  
  9. int main(int argc, string argv[])
  10. {
  11.     //string cipher = argv[1];
  12.  
  13.     //Validate the key
  14.     bool hasRepeats = false;
  15.     bool nonletters = false;
  16.  
  17.     //Make sure theres a key
  18.     if (argc != 2)
  19.     {
  20.         printf("Usage: ./substitution key\n");
  21.         return 1;
  22.     }
  23.  
  24.     for (int i = 0, n = strlen(argv[1]); i < n; i++)
  25.     {
  26.         for (int j = 0, m = strlen(argv[1]); j < n; j++)
  27.         {
  28.             if (i == j)
  29.             {
  30.                 //comparing to self, do nothing
  31.             }
  32.             else
  33.             {
  34.                 // Check for repeated letters
  35.                 if ((toupper(argv[1][i])) == toupper(argv[1][j]))
  36.                 {
  37.                     hasRepeats = true;
  38.                 }
  39.             }
  40.             //return 0;
  41.         }
  42.         // Check for numbers in the argv[1]
  43.         if (isalpha(argv[1][i]) == 0)
  44.         {
  45.             nonletters = true;
  46.         }
  47.     }
  48.  
  49.     if (argc == 2 && strlen(argv[1]) != 26)
  50.     {
  51.         printf("Key must contain 26 characters.\n");
  52.         return 1;
  53.     }
  54.     else if (nonletters == true)
  55.     {
  56.         printf("Key must only contain alphabetic characters.\n");
  57.         return 1;
  58.     }
  59.     else if (hasRepeats == true)
  60.     {
  61.         printf("Key must not contain repeated characters.\n");
  62.         return 1;
  63.     }
  64.     else
  65.     {
  66.         // printf("Looks good, baby!\n");
  67.     }
  68.  
  69.     //Ask for the user text
  70.     string plaintext = get_string("plaintext: \n");
  71.  
  72.     //Encipher the text
  73.     string ciphertext = encipher(plaintext, argv[1]);
  74.  
  75.     //Print the ciphertext
  76.     printf("ciphertext: %s\n", ciphertext);
  77.  
  78.     return 0;
  79. }
  80.  
  81. string encipher(string plaintext, string cipher)
  82. {
  83.     char encoded[strlen(plaintext)];
  84.  
  85.     for (int i = 0, n = strlen(plaintext); i < n; i++)
  86.     {
  87.         bool lowerCase = false;
  88.  
  89.         if (isalpha(plaintext) == true)
  90.         {
  91.             if (islower(plaintext[i]) == true)
  92.             {
  93.                 lowerCase = true;
  94.             }
  95.  
  96.             int position = toupper(plaintext[i]) - 65;
  97.  
  98.             encoded[i] = cipher[position];
  99.  
  100.             //correct to lowercase
  101.             if (lowerCase == true)
  102.             {
  103.                 encoded[i] = tolower(encoded[i]);
  104.             }
  105.         }
  106.         else
  107.         {
  108.             encoded[i] = plaintext[i];
  109.         }
  110.     }
  111.     return encoded;
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement