Advertisement
noob339

Untitled

Dec 10th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 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.  
  8. int main(int argc, string argv[])
  9. {
  10.     int n = 0, tLength = 0;
  11.     string text = get_string("Plaintext: ");
  12.    
  13.     //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
  14.     //Until eventually it can receive the plain text and encipher it.
  15.     if (argc != 2)
  16.     {
  17.         printf("Usage: ./substitution key\n");
  18.         return 1;
  19.     }
  20.     else
  21.     {
  22.         //here we get the legnth of the string
  23.         n = strlen(argv[1]);
  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.    
  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.     for (int i = 0; i < n; i++)
  36.     {
  37.         if (isdigit(argv[1][i]))
  38.         {
  39.             printf("Error, must contain only characters\n");
  40.             return 1;
  41.         }
  42.     }
  43.                
  44.     //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
  45.     for (int j = 0; j < n - 1; j++)
  46.     {
  47.         for (int f = 1; f < n - j; f++)
  48.         {
  49.             if (argv[1][j] == argv[1][f + j])
  50.             {
  51.                 printf("Error, characters cannot repeat\n");
  52.                 return 1;
  53.             }
  54.         }
  55.        
  56.     }
  57.    
  58.     //time to encipher, but do I abstract this, how do I get multi dimensional arrays as parameters on functions?
  59.    
  60.     //here we encipher the text and perform several checks before actually enchiphering
  61.    
  62.     int position;
  63.    
  64.     for (int i = 0; i < n - 1; i++)
  65.     {
  66.         if (isalpha(text[i]) && isupper(text[i]))
  67.         {
  68.             text[i] = text[i] - 65;
  69.             position = (int)(text[i]);
  70.             text[i] = argv[1][position];
  71.             text[i] = toupper(text[i]);
  72.         }
  73.         else if (isalpha(text[i]) && islower(text[i]))
  74.         {
  75.             text[i] = text[i] - 97;
  76.             position = (int)(text[i]);
  77.             text[i] = argv[1][position];
  78.             text[i] = tolower(text[i]);
  79.         }
  80.     }
  81.    
  82.     //here we get the length of the text
  83.     tLength = strlen(text);
  84.    
  85.    
  86.     //here we print our the cipher text
  87.     printf("Ciphertext: ");
  88.    
  89.     for (int i = 0; i < tLength; i++)
  90.     {
  91.         printf("%c", text[i]);
  92.     }
  93.     printf("\n");
  94.    
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement