Advertisement
decs4usa

vigenere

Nov 10th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. int shift(char c);
  8.  
  9. int main(int argc, string argv[])
  10.  
  11. {
  12.     if (argc != 2) //if user doesn't run command-line argument of one or enter positive int  
  13.     {
  14.         printf("Usage: ./vigenere key\n"); //prompt them to run it again
  15.         return 1; //returning zero means everything went well
  16.     }
  17.    
  18.     for (int j = 0, n = strlen(argv[1]); j < n; j++)  //length of the entire string. Save n as strlen for use in loop
  19.     {
  20.         if (isalpha(argv[1][j])) //if user doesn't run command-line argument of one or enter positive int and user inputs an alpha key, continue
  21.  
  22.         {
  23.             //printf("%c\n", argv[1][j]); //Print after collecting plaintext
  24.         }
  25.  
  26.    
  27.         else
  28.  
  29.         {      
  30.             printf("Usage: ./vigenere key\n"); //prompt them to run it again
  31.             return 1; //returning zero means everything went well
  32.         }
  33.  
  34.     }  
  35.    
  36.     printf("ciphertext: ");
  37.        
  38.     string p = get_string("plaintext: "); //prompt for plaintext and store
  39.     string key = argv[1]; //declare the key as a string
  40.     char cipher; //declare cipher as a char
  41.        
  42.     //NOW ROTATE BY THE KEY
  43.    
  44.     for (int i = 0; j = 0; i < strlen(p); i ++) //length of the entire string
  45.  
  46.     {  
  47.         if (j == strlen(key)) //if j reaches the end of the string, start over
  48.  
  49.             {
  50.                 j = 0;
  51.             }
  52.    
  53.         if (isupper(p[i]))
  54.                
  55.         {
  56.             cipher = (((p[i] + shift(key[j])) - 65) % 26) + 65; //Use the new shift function now
  57.             printf("%c", cipher); //print ciphertext after rotating
  58.             j++;
  59.         }
  60.    
  61.         else if (islower(p[i]))
  62.                
  63.         {
  64.             cipher = (((p[i] + shift(key[j])) - 97) % 26) + 97; //add key and rotate
  65.             printf("%c", cipher); //print ciphertext after rotating
  66.             j++;
  67.         }
  68.              
  69.         else
  70.            
  71.         {
  72.             printf("%c", p[i]);
  73.         }
  74.        
  75.     }
  76.    
  77.     printf("\n");
  78.     return 0;    
  79.    
  80.    
  81. } //Closing braces
  82.  
  83.  
  84.  
  85. //function definition
  86. int shift(char c); //NEW SHIFT function
  87. {
  88.     int k;
  89.  
  90.     if (islower(c))
  91.         {
  92.             k = c - 97;
  93.         }
  94.    
  95.     if (isupper(c))
  96.         {
  97.             k = c - 65;
  98.         }
  99.    
  100.     return k;
  101.                
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement