Advertisement
decs4usa

vigenere

Nov 9th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 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[1]);
  10.     int j;
  11.    
  12. {
  13.     if (argc != 2) //if user doesn't run command-line argument of one or enter positive int  
  14.     {
  15.         printf("Usage: ./vigenere key\n"); //prompt them to run it again
  16.         return 1; //returning zero means everything went well
  17.     }
  18.    
  19.     if (isalpha(argv[1])) //If user inputs an alpha key continue
  20.  
  21.         string p = get_string("plaintext: "); //prompt for plaintext and store
  22.    
  23.         {
  24.             printf("ciphertext: "); //Print after collecting plaintext
  25.         }
  26.  
  27.         for (int j = 0, n = strlen(argv[1];  j < n; j++)  //length of the entire string. Save n as strlen for use in loop
  28.         {  
  29.             char cipher; //char within the string
  30.             int shift(char cipher);
  31.  
  32.             if (isupper(p[i]))
  33.  
  34.             {
  35.                 cipher = ((p[i] + key[j]) % 26); //add key and rotate
  36.                 printf("%c", cipher); //print ciphertext after rotating
  37.             }
  38.  
  39.             else if (islower(p[i]))
  40.  
  41.             {
  42.                 cipher = ((p[i] + key[j]) % 26); //add key and rotate
  43.                 printf("%c", cipher); //print ciphertext after rotating
  44.             }
  45.  
  46.             else
  47.  
  48.             {
  49.                 printf("%c", p[i]); //Else, print unchanged
  50.             }
  51.  
  52.         }
  53.         printf("\n");
  54.         return 0;
  55.      
  56.    
  57. } //Closing brackets
  58.  
  59. //function definition
  60.     int shift(char c);
  61.         int j;
  62.     {
  63.         for (j = 0; j < strlen(argv[1]); j++)
  64.             int c = (argv[1]); //NEW SHIFT function; user input at start of program
  65.             int key = (argv[1][j]);
  66.  
  67.                 for (key[j] % strlen(argv[1]))
  68.  
  69.                     if (islower(c))
  70.                     {
  71.                         key = ((key[j] - 97) % 26); //Shifting by alpha index
  72.                     }
  73.                     if (isupper(c))
  74.                     {
  75.                         key = ((key[j] - 65) % 26);
  76.                     }
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement