Advertisement
Guest User

vigenere

a guest
Aug 27th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. int main(int argc, string argv[])
  8. {
  9.     if (argc != 2 || argv[1] == NULL )
  10.     {
  11.         return 1;
  12.     }
  13.     string keyword = argv[1];
  14.    
  15.     for (int j = 0, n = strlen(keyword); j < n; j++)
  16.     {
  17.         if ( !isalpha(keyword[j]) && argv[1] = NULL)
  18.         {
  19.             printf("Keyword must only contain letters A-Z and a-z\n");
  20.             return 1;
  21.         }
  22.         else
  23.         {
  24.                 string plaintext = GetString();
  25.                
  26.                 for (int i = 0, n = strlen(plaintext); i < n; i++)
  27.                 {
  28.                
  29.                     if (isalpha(plaintext[i]))
  30.                     {  
  31.                        
  32.                         int code_lower = (keyword[j] - 'a');
  33.                         int code_upper = (keyword[j] - 'A');
  34.                         int letter = plaintext[i];
  35.                         int result_lower = (letter - 'a' + (code_lower %26)) + 'a';
  36.                         int result_upper = (letter - 'A' + (code_upper %26)) + 'A';
  37.                        
  38.                         if (islower(plaintext[i]))
  39.                         {
  40.                             if (result_lower > 'z')
  41.                             {
  42.                                 int result_alphlower = (result_lower - 'z') + 96;
  43.                                 printf("%c", result_alphlower);
  44.                             }      
  45.                             else
  46.                             {
  47.                                 printf("%c", result_lower);
  48.                             }
  49.                         }
  50.                         if (isupper(plaintext[i]))
  51.                         {
  52.                             if (result_upper > 'Z')
  53.                             {
  54.                                 int result_alphupper = (result_upper - 'Z') + 64;
  55.                                 printf("%c", result_alphupper);
  56.                             }
  57.                             else
  58.                             {      
  59.                                 printf("%c", result_upper);
  60.                             }
  61.                         }
  62.                     }
  63.                     else
  64.                     {
  65.                         printf("%c", plaintext[i]);
  66.                     }                    
  67.                 }
  68.                 printf("\n");
  69.                 return 0;
  70.         }  
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement