Advertisement
Sammyxfr

CS50 - WEEK 2 PSET - SUBSTITUTION

Mar 17th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | Source Code | 0 0
  1. #include <cs50.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. int quit(void);
  7. char *hash(string argv, string plaintext, string encodedtext);
  8. int calc_checksum(void);
  9.  
  10. int main(int argc, string argv[])
  11. {
  12.     int checksum = calc_checksum();
  13.     int hashsum = 0;
  14.  
  15.     if (argc != 2 || strlen(argv[1]) != 26) // Basic checks for arguments
  16.     {
  17.         return quit();     // Exit case 1 - if argument count mismatch
  18.     }
  19.     else
  20.     {
  21.         for (int i = 0; i < 26; i++)
  22.         {
  23.             if (isalpha(argv[1][i]))
  24.             {
  25.                 hashsum += toupper(argv[1][i]); // To save me the hassle i have calculated the hashes in uppercase
  26.             }
  27.             else
  28.             {
  29.                 return quit();     // Exit case 2 - if Any number is in the key
  30.             }
  31.         }
  32.         if (hashsum == checksum)
  33.         {
  34.             string plaintext = get_string("plaintext:  ");
  35.             char encodedtext[strlen(plaintext)];
  36.  
  37.             printf("ciphertext: %s\n", hash(argv[1], plaintext, encodedtext));
  38.         }
  39.         else
  40.         {
  41.             return quit();    // Exit case 3 - if the hashes dont match
  42.         }
  43.     }
  44. }
  45.  
  46. int quit(void)
  47. {
  48.     printf("Usage ./substitution Key\n");
  49.     return 1;
  50. }
  51.  
  52. char *hash(string argv, string plaintext, string encodedtext)
  53. {
  54.     char key[26];
  55.  
  56.     for (int i = 0; i < strlen(plaintext); i++)
  57.     {
  58.         if (isupper(plaintext[i]))
  59.         {
  60.             for (int n = 65, j = 0; n <= 90; j++, n++)
  61.             {
  62.                 if (plaintext[i] == n)   // This figures out which alphabet it is
  63.                 {
  64.                     encodedtext[i] =
  65.                         toupper(argv[j]); // This figures out what key value to be replaced with
  66.                 }
  67.                 else
  68.                 {
  69.                     continue;
  70.                 }
  71.             }
  72.         }
  73.         else if (isdigit(plaintext[i]) || !(isalpha(plaintext[i])))
  74.         {
  75.             encodedtext[i] = plaintext[i];     // no replacement happens
  76.  
  77.         }
  78.         else
  79.         {
  80.             for (int n = 97, j = 0; n <= 122; j++, n++)
  81.             {
  82.  
  83.                 if (plaintext[i] == n)
  84.                 {
  85.                     encodedtext[i] =
  86.                         tolower(argv[j]); // This figures out what key value to be replaced with
  87.  
  88.                 }
  89.                 else if(plaintext[i] == ' ') {
  90.                     encodedtext[i] = ' ';      // For not neglecting the space in the user text
  91.                 }
  92.                 else
  93.                 {
  94.                     continue;
  95.                 }
  96.             }
  97.         }
  98.     }
  99.  
  100.     encodedtext[strlen(plaintext)] = '\0';
  101.     return encodedtext;
  102. }
  103.  
  104. int calc_checksum(void)
  105. {
  106.     int sum = 0;
  107.     for (int i = 65; i < 91; i++)
  108.     {
  109.         sum += i;
  110.     }
  111.     return sum;
  112. }
  113.  
Tags: CS50- week 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement