Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. int shift(char c);
  6. int main(int argc, string argv[])
  7. {
  8. // Check to see if two arguments are enterted at launch
  9.      int cipher = 0;
  10.     if (argc != 2)
  11.     {
  12.         // If not return error & return 0
  13.         printf("Usage: ./vigenere keyword \n");
  14.         return 1;
  15.     }
  16.     else
  17.     {
  18.         int strlength = strlen(argv[1]);
  19.         // Iterates through characters in second argument (key), checking to see if they are digits
  20.         for (int k = 0; k < strlength; k++)
  21.         {
  22.             if (isdigit(argv[1][k]))
  23.             {
  24.                 // If not return error & return 1
  25.                 printf("Usage: ./vigenere keyword\n");
  26.                 return 2;
  27.             }
  28.         }
  29.             //char *c =argv[1];
  30.             string plaintext = get_string("Plaintext: ");
  31.             int len = (int) strlen(plaintext);
  32.             //int b = atoi(c);
  33.             //char code[len];
  34.             //strcpy (code, plaintext);
  35.              int z=0;
  36.              for (int j = 0; j < len; j++)  
  37.              {
  38.                  
  39.                     int key = shift(argv[1][z]);
  40.                  printf("%i",z);
  41.                   if (isupper(argv[1][z]))
  42.                     {
  43.                       //printf("theory\n");
  44.                       cipher = ((((plaintext[j] - 'A') + key) + 'A'));
  45.                       //cipher = ((((plaintext[j] - 'A') + key) % 26) + 'A');
  46.                       //printf("%c", (((plaintext[j] - 'A') + key) % 26) + 'A');
  47.                      //printf("%i",z);
  48.                      
  49.                      
  50.                       printf("%c",cipher);
  51.                        z++;
  52.                       if (z > strlen(argv[1])-1)
  53.                          {
  54.                              z=0;
  55.                          }
  56.                      }
  57.                    
  58.                       if (islower(argv[1][z]))
  59.                         {
  60.                    
  61.                         //printf("theory\n");  
  62.                         cipher = (((plaintext[j] - 'a') + key) + 'a');
  63.                          //cipher = ((((plaintext[j]) -'a') + key) % 26) + 'a');
  64.                          //printf("%c", (((plaintext[j] - 'a') + key) % 26) + 'a');
  65.                        
  66.                          //printf("%i",z);
  67.                          
  68.                           printf("%c",cipher);  
  69.                           z++;
  70.                        if (z > strlen(argv[1])-1)
  71.                          {
  72.                              z=0;
  73.                          }
  74.                       }
  75.                       //z++;    
  76.                      //else if (!isalpha(plaintext[j]))
  77.                         //{
  78.                             //printf("%c", plaintext[j]);  
  79.                          //}
  80.                        
  81.                    
  82.                      
  83.                      
  84.                     /* else
  85.                      {
  86.                      z++;
  87.                      }
  88.                    
  89.                      else
  90.                      {
  91.                      z++;
  92.                      }
  93.                      */
  94.                      //z++;
  95.                   }
  96.              
  97.                
  98.                      
  99.                 printf("\n");
  100.            
  101.                  }
  102. }
  103.     int shift(char c)
  104.     {
  105.         int i = c;
  106.         if (i <= 'Z' && i >= 'A')
  107.         {
  108.            return ((i - 'A') % 26);
  109.         }
  110.         else
  111.         {
  112.             return ((i - 'a') % 26);
  113.         }
  114.        
  115.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement