Advertisement
Guest User

cs50 pset2 vignere error

a guest
Sep 12th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <cs50.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #define ALPHABET 26
  6.  
  7. int main(int argc, string argv[])
  8. {
  9.     if (argc !=2)                                                                       // invalid if there are more than two arguments
  10.     {
  11.         printf("Enter two arguments only.\n");
  12.         return 1;
  13.     }
  14.     else
  15.     {
  16.         for (int i = 0, j = strlen(argv[1]); i < j; i++)                                // loop through each character of key to ensure its all alphabets
  17.         {
  18.             if (isalpha(argv[1][i]))
  19.             {
  20.                 printf("Re-enter key with alphabets only.\n");
  21.                 return 1;
  22.             }
  23.         }
  24.     }
  25.  
  26.     string key = argv[1];                                                               // bind argument 1 to key variable
  27.     string plaintext = get_string("plaintext: \n");                                     // prompt user for plaintext
  28.  
  29.     for (int i = 0, int j = 0, int pLength = strlen(plaintext); i < pLength; i++)
  30.     {
  31.  
  32.         int keyLength = strlen(key);                                                    // get length of key
  33.         char pLetter = plaintext[i];                                                    // bind each character of plaintext to pLetter
  34.         char kLetter = argv[1][j % keyLength];                                          // bind each character of key to kLetter
  35.  
  36.         if (isupper(kLetter))                                                           // get alphabet index of key if its uppercase
  37.         {
  38.             kLetter -= 65;
  39.         }
  40.  
  41.         else if (islower(kLetter))                                                      // get alphabet index of key if its lowercase
  42.         {
  43.             kLetter -= 97;
  44.         }
  45.  
  46.         if (isupper(plaintext[i]))                                                      // add key to upper plaintext character, deduct 65, module by 26 and add back 65 to get ASCII value
  47.         {
  48.             printf("%c", (((plaintext[i] + kLetter - 65) % ALPHABET) + 65);
  49.             j++;                                                                        // increment j by 1 to loop through each character of key
  50.         }
  51.  
  52.         else if (islower(plaintext[i]))                                                 // add key to lower plaintext character, deduct 97, module by 26 and add back 97 to get ASCII value
  53.         {
  54.             printf("%c", (((plaintext[i] + kLetter - 97) % ALPHABET) + 97);
  55.             j++;
  56.         }
  57.         else
  58.         {
  59.             printf("%c", plaintext[i]);                                                 // if plaintext is neither upper or lower, print character as is
  60.         }
  61.     }
  62.     printf("\n");
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement