Advertisement
decs4usa

vigenere

Nov 10th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 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 key);
  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.     string p = get_string("plaintext: "); //prompt for plaintext and store
  37.    
  38.     //NOW ROTATE BY THE KEY
  39.    
  40.     for (int i = 0; i < strlen(p); i++) //length of the entire string
  41.     {  
  42.         int shift(char key); //char within the string
  43.        
  44.         if (isupper(p[i]))
  45.                
  46.         {
  47.             cipher = (((p[i] + key[j]) - 65) % 26) + 65; //add key and rotate
  48.             printf("ciphertext: %c", cipher); //print ciphertext after rotating
  49.         }
  50.    
  51.         else if (islower(p[i]))
  52.                
  53.         {
  54.             cipher = (((p[i] + key[j]) - 97) % 26) + 97; //add key and rotate
  55.             printf("ciphertext: %c", cipher); //print ciphertext after rotating
  56.         }
  57.              
  58.         else
  59.            
  60.         {
  61.             printf("ciphertext: %c", p[i]);
  62.         }
  63.                
  64.     }
  65.     printf("\n");
  66.     return 0;    
  67.    
  68.    
  69. } //Closing braces
  70.  
  71.  
  72.  
  73. //function definition
  74. int shift(char key); //NEW SHIFT function
  75. {
  76.     int key = atoi(argv[1][j]); //user input at start of program
  77.    
  78.     for (key[j] % strlen(argv[1])) //wrap keyword, count off method
  79.  
  80.     {
  81.         if (isupper(key))
  82.         key = (key[j] - 65); //Shifting by alpha index
  83.                    
  84.         if (islower(key))
  85.         key = (key[j] - 97); //Shifting by alpha index
  86.     }
  87.                        
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement