SHARE
TWEET

Untitled

a guest Oct 16th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int k;
  7. int shift(char c);
  8. int main(int argc, string argv[])
  9. {
  10.     if (argc == 2) // ensure one argument from user
  11.     {
  12.         for (int i = 0; i < strlen(argv[1]); i++) // iterate over length of argument input by user
  13.         {
  14.             if (isalpha(argv[1][i]) == 0) // ensure each character in argument is alphabetic
  15.             {
  16.                 printf("Usage: ./vigenere keyword\n"); // error/failure message to user
  17.                 return 1;
  18.             }
  19.         }
  20.     }
  21.        
  22.     string plain = get_string("plaintext: "); // get plaintext input from user
  23.     printf("ciphertext: ");
  24.     for (int i = 0; i < strlen(plain); i++) // iterate over length of plaintext input
  25.     {
  26.         k = shift(argv[1][i]);
  27.         char c = plain[i]; // convert string to individual characters
  28.         if (c < 65 || (c > 90 && c < 97) || c > 122) // leave non-alphabetical characters unchanged
  29.         {
  30.             printf("%c", c);
  31.         }
  32.         else if (((c + k % 26) > 90 && (c + k % 26) < 97) || (c + k % 26) > 122)
  33.         // prevent printing of undesired non-alphabetical characters resulting from rotation
  34.         {
  35.             printf("%c", c + (k % 26 - 26));
  36.         }
  37.         else    
  38.         {
  39.             printf("%c", c + (k % 26)); // apply key upon print
  40.         }
  41.         printf("\n");                    
  42.     }
  43. }    
  44.    
  45. int shift(char c)
  46. {
  47.     if (isupper(c))
  48.     {
  49.         int x = (c - 65);
  50.         return x;
  51.     }
  52.     else
  53.     {
  54.         int x = (c - 97);
  55.         return x;
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top