Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #import <stdio.h>
  2. #import <cs50.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. int main (int argc, string argv[])
  8.  
  9. int i;
  10.  
  11. {
  12.    
  13.     //checking if te input has 2 arguments
  14.     if (argc != 2 ) { printf("try to use 2 arguments\n"); return 1;
  15.     }
  16.     //convert argv to string "key"
  17.     string key = argv[1];
  18.  
  19.     if (argc == 2)
  20.     {
  21.         //plaintext input
  22.     string text = get_string("Enter plaintext: ");
  23.         printf("ciphertext: ");
  24.    
  25.         // print out the plaintext letter by letter    
  26.         for (int i =0, n = strlen(text); i < n ; i++)
  27.                    
  28.          // here is where I struggle, the sub script to wraparound the key only if its an alphabetic character.  
  29.         if isalpha(text[i])  {
  30.           for (int keyindex = 0, nk = strlen(key); keyindex < nk; keyindex++)
  31.             keyindex % nk;
  32.              }
  33.              
  34.              // convert the plaintext to ciphertext
  35.              
  36.                 if islower(text[i])
  37.                     printf("%c", (((text[i] + key[keyindex]) - 97) % 26) + 97);
  38.                 else if isupper(text[i])
  39.                     printf("%c", (((text[i] + key[keyindex]) - 65) % 26) + 65);
  40.                    
  41.                 else printf("%c",text[i]);
  42.                
  43.         printf("\n");
  44.     }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement