ssoni

substitution_v1.c

Mar 11th, 2022 (edited)
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main(int argc, string argv[])
  7. {
  8.  
  9.     //must provide key
  10.     if (argc != 2)
  11.     {
  12.         printf("USAGE: ./substitution 26KEY\n");
  13.         return 1;
  14.     }
  15.  
  16.     //store key in a new variable
  17.     string key = argv[1];
  18.  
  19.     //check if key is 26 long
  20.     if (strlen(key) != 26)
  21.     {
  22.         printf("Key must contain 26 characters.\n");
  23.         return 1;
  24.     }
  25.  
  26.     //check if all 26 characters are acctually letters
  27.     for (int i=0; i<strlen(key); i++)
  28.     {
  29.         if (isalpha(key[i]) == 0)
  30.         {
  31.             printf("Key must contain letters only.\n");
  32.             return 1;
  33.         }
  34.     }
  35.  
  36.     //check if any letters are repeated
  37.     //check each letter and keep track in an array called "used"
  38.     //eg:  When you see the letter A in the key, you set position 1 as true
  39.     //if its already set to true, that means you already had that letter in the key
  40.     bool used[26] = {};
  41.     int pos=0;
  42.     for (int i=0; i<strlen(key); i++)
  43.     {
  44.         pos=toupper(key[i])-65;
  45.         if (used[pos] == true)
  46.         {
  47.             printf("Key can't repeat letters.\n");
  48.             return 1;
  49.         }
  50.         else
  51.         {
  52.             used[pos] = true;
  53.         }
  54.     }
  55.  
  56.     string plaintext="";
  57.  
  58.     //get word to encrypt
  59.     plaintext = get_string("plaintext: ");
  60.  
  61.     //how to substitute each letter using the 26KEY?
  62.     //loop through the entire word...
  63.     //get position of each letter (eg: a=1, b=2, etc)
  64.     //then look up that position in the 26KEY and print that letter
  65.     int position=0;
  66.     char oldChar;
  67.     char newChar;
  68.  
  69.     printf("ciphertext: ");
  70.  
  71.     for (int i=0; i<strlen(plaintext); i++)
  72.     {
  73.         oldChar = plaintext[i];
  74.  
  75.         //only translate letters
  76.         if (isalpha(oldChar) == 0)
  77.         {
  78.             printf("%c", oldChar);
  79.         }
  80.         else
  81.         {
  82.             //translate a-z to 0-25 position to look up in the key
  83.             position = toupper(oldChar)-65;
  84.             newChar = key[position];
  85.  
  86.             //preserve the case of the original letter, regardless of case in the key
  87.             //ie:  lower stays lower, upper stays upper
  88.             if (islower(oldChar))
  89.             {
  90.                 newChar=tolower(newChar);
  91.             }
  92.             else
  93.             {
  94.                 newChar=toupper(newChar);
  95.             }
  96.             printf("%c",newChar);
  97.         }
  98.     }
  99.     printf("\n");
  100. }
Add Comment
Please, Sign In to add comment