Advertisement
jamestha3d

cs50 substitution

Sep 26th, 2022
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. char char_index(char c);
  7. int main(int argc, string argv[])
  8. {
  9.     //ensure correct amount of args
  10.     if (argc != 2)
  11.     {
  12.         printf("Usage: ./substitution key\n");
  13.         return 1;
  14.     }
  15.  
  16.     string key = argv[1];
  17.     int key_length = strlen(key);
  18.  
  19.     //check if key has exactly 26 chars
  20.     if (key_length != 26)
  21.     {
  22.         printf("Key must contain 26 characters.\n");
  23.         return 1;
  24.     }
  25.  
  26.     //check if key has only alphabetic chars and no repeating char
  27.     for (int i = 0; i < key_length; i++)
  28.     {
  29.         if (!isalpha(key[i]))
  30.         {
  31.             printf("key must be contain only alphabetic characters");
  32.             return 1;
  33.         }
  34.  
  35.         //double check, literally, if a char has been seen before
  36.         for (int j = 0; j < key_length; j++)
  37.         {
  38.             if (i != j)
  39.             {
  40.                 if (key[i] == key[j])
  41.                 {
  42.                     printf("key must contain unique characters");
  43.                     return 1;
  44.                 }
  45.             }
  46.  
  47.         }
  48.     }
  49.     string plaintext = get_string("Plaintext: ");
  50.     int length = strlen(plaintext);
  51.     string ciphertext[length];
  52.  
  53.     printf("ciphertext: ");
  54.     for (int i = 0; i < length; i++)
  55.     {
  56.         //if this letter is a char
  57.         char plain_char = plaintext[i];
  58.         if (isalpha(plain_char))
  59.         {
  60.             int index = char_index(plaintext[i]);
  61.             char cipher_char = key[index];
  62.             if (isupper(plain_char))
  63.             {
  64.                 cipher_char = toupper(cipher_char);
  65.             }
  66.             else
  67.             {
  68.                 cipher_char = tolower(cipher_char);
  69.             }
  70.             printf("%c", cipher_char);
  71.         }
  72.         else
  73.         {
  74.             printf("%c", plain_char);
  75.         }
  76.  
  77.     }
  78.     printf("\n");
  79. }
  80.  
  81. //get the index of each char to be used in the key
  82. char char_index(char c)
  83. {
  84.     int index = 0;
  85.     //determine the positional value of char
  86.     if (isupper(c))
  87.     {
  88.         index = ((int) c) - 65;
  89.     }
  90.     else if (islower(c))
  91.     {
  92.         index = ((int) c) - 97;
  93.     }
  94.     return index;
  95.     //use this to determine the value of cipherchar
  96.     //return cipher
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement