Advertisement
Guest User

vigenere

a guest
Nov 17th, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. int main(int argc, string argv[])
  8. {
  9.  
  10. //verify that have the correct number of arguments
  11.     if (argc!=2){
  12.         printf("Please dont forget to add ONE alphabetical argument\n");
  13.         return 1;
  14.         }      
  15.     string plaintext;
  16.     string key = argv[1];
  17.  
  18. //verify that the key is alphabetical    
  19.     if (isalpha(key)) {
  20.         plaintext = GetString();
  21.          }
  22.     else {
  23.         printf("Please use am alphabetical key argument\n");
  24.         return 1;
  25.         }
  26.        
  27.      
  28. //encipher here
  29.     int nonalpha = 0;
  30.     int keylength = strlen(key);
  31.     int keynum;
  32.     for (int i = 0, n=strlen(plaintext); i < n; i++)
  33.     {
  34.         int keyposition = (i - nonalpha) % keylength;
  35.         if (islower(key[keyposition])){
  36.                 keynum = (int)(key[keyposition] -97);
  37.             }
  38.         else {
  39.                 keynum = (int)(key[keyposition] -65);
  40.             }    
  41.              
  42.         if (isalpha (plaintext[i])){
  43.             if (islower(plaintext[i])){
  44.                 int c = (((plaintext[i] - 97 + keynum) %26) +97);
  45.                 printf("%c", c);
  46.                 }
  47.             else {
  48.                 int c = (((plaintext[i] - 65 + keynum) %26) +65);
  49.                 printf("%c", c);
  50.                 }
  51.             }
  52.         else {
  53.             printf("%c", plaintext[i]);
  54.             nonalpha = nonalpha + 1 ;
  55.             }
  56.     }
  57.    
  58.     printf("\n");
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement