Advertisement
noob339

Untitled

Dec 10th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.    
  7. int main(int argc, string argv[])
  8. {
  9.     int n = 0, tLength = 0;
  10.     string text;
  11.    
  12.     //here we check if the command line receives 2 arguments, if it does not we display and error, for each time it passes without error it dives further
  13.    
  14.     if (argc != 2)
  15.     {
  16.         printf("Usage: ./substitution key\n");
  17.         return 1;
  18.     }
  19.     else
  20.     {
  21.         //here we get the legnth of the string
  22.         n = strlen(argv[1]);
  23.        
  24.     }
  25.        
  26.     //Here we are checking if the key has a length of 26, if it does not, we display an error message
  27.     if (n != 26)
  28.     {
  29.         printf("Error, must contain 26 characters\n");
  30.         return 1;
  31.     }
  32.     else
  33.     {
  34.         //Here we loop through the array to check if its a digit or not, we only want letters, so if its a digit, an error message is displayed
  35.                
  36.         for (int i = 0; i < n; i++)
  37.         {
  38.             if (isdigit(argv[1][i]))
  39.             {
  40.                 printf("Error, must contain only characters\n");
  41.                 return 1;
  42.             }
  43.         }
  44.        
  45.         //Here we check to see if there are letters that repeat, boy this was a tricky one, but solved it with pen a paper and the ole noggin, felt good
  46.        
  47.         for (int j = 0; j < n - 1; j++)
  48.         {
  49.             for (int f = 1; f < n - j; f++)
  50.             {
  51.                 if (argv[1][j] == argv[1][f + j])
  52.                 {
  53.                     printf("Error, characters cannot repeat\n");
  54.                     return 1;
  55.                 }
  56.             }
  57.            
  58.         }
  59.        
  60.        
  61.         text = get_string("Plaintext: ");
  62.        
  63.         int position;
  64.    
  65.         tLength = strlen(text);
  66.        
  67.         //here we encipher the text and perform several checks before actually enchiphering
  68.        
  69.         for (int i = 0; i < tLength; i++)
  70.         {
  71.             if (isalpha(text[i]) && isupper(text[i]))
  72.             {
  73.                 text[i] = text[i] - 65;
  74.                 position = (int)(text[i]);
  75.                 text[i] = argv[1][position];
  76.                 text[i] = toupper(text[i]);
  77.             }
  78.             else if (isalpha(text[i]) && islower(text[i]))
  79.             {
  80.                 text[i] = text[i] - 97;
  81.                 position = (int)(text[i]);
  82.                 text[i] = argv[1][position];
  83.                 text[i] = tolower(text[i]);
  84.             }
  85.         }
  86.        
  87.         printf("ciphertext: ");
  88.    
  89.         for (int i = 0; i < tLength; i++)
  90.         {
  91.             printf("%c", text[i]);
  92.         }
  93.         printf("\n");
  94.        
  95.     }
  96.    
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement