Advertisement
Guest User

Vigenere (Not complete)

a guest
Apr 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6.  
  7. int main(int argc, string argv[]) {
  8.    
  9.     // Check to make sure only 1 thing is in the command-line
  10.     if (argc != 2) {
  11.        
  12.         printf("Please enter in the command-line a string of alphabetical characters to compose your key.\n");
  13.         return 1;
  14.        
  15.     }
  16.        
  17.         // Get the plaintext
  18.         printf("plaintext: ");
  19.         string plainText = get_string();
  20.        
  21.         // Take the key convert to string and make var j which will cycle through the key
  22.         string key = argv[1];
  23.         int j = 0;
  24.        
  25.    
  26.         if (plainText != NULL) {
  27.            
  28.             printf("ciphertext: ");
  29.            
  30.             // Cycle through the plaintext that the user inputed
  31.             for (int i = 0, n = strlen(plainText); i < n; i++) {
  32.                    
  33.                     // If it's a letter
  34.                     if (isalpha(plainText[i])) {
  35.                        
  36.                         // Length of pText % of the length of the key so once it maxes out the key will reset
  37.                         if (strlen(plainText) % strlen(key) == strlen(key)) {
  38.                    
  39.                         j = 0;
  40.                    
  41.                          }
  42.                        
  43.                         // Lowerase letters first
  44.                         if (islower(plainText[i])) {
  45.                        
  46.                             char lower = ((((plainText[i]) - 97) + key[j]) % 26) + 97;
  47.                             printf("%c", lower);
  48.                             j++;
  49.                        
  50.                         }
  51.                        
  52.                         // Uppercase letters
  53.                         else if (isupper(plainText[i])) {
  54.                    
  55.                             char upper = ((((plainText[i]) - 65) + key[j]) % 26) + 65;
  56.                             printf("%c", upper);
  57.                             j++;
  58.                        
  59.                         // Spaces/punctuation
  60.                         } else {
  61.                        
  62.                             printf("%c", plainText[i]);
  63.                        
  64.                         }
  65.                     }
  66.                    
  67.             }
  68.            
  69.             // Make things look clean and return 0 to show things are all good
  70.             printf("\n");
  71.             return 0;
  72.            
  73.         }
  74.    
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement