Advertisement
Eastkap

Vigenere CS50 2015

Feb 20th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. int main (int agrc,string argv[])
  8. {
  9.     if (agrc!=2)
  10.     {
  11.         printf("Please specify one command argument, being the cypher key");
  12.         return 1;
  13.     }
  14.     string key = agrv[1];
  15.     for (int i=0, j=strlen(key); i<j; i++)
  16.     {
  17.         key[i]=tolower(key[i]);
  18.     }
  19.     string plaintext=GetString();
  20.     int letter;
  21.     for (int i=0, j=strlen(key);i<j;i++)
  22.     {
  23.         if (isalpha(plaintext[i]))
  24.         {
  25.             if(isupper(plaintext[i]))
  26.             {
  27.                 letter= (int)'A'+(((int)plaintext[i]-(int)'A'+((int)key[i%5]-(int)'a'))%26);
  28.             }
  29.             else
  30.             {
  31.                 letter= (int)'a'+(((int)plaintext[i]-(int)'a'+((int)key[i%5]-(int)'a'))%26);
  32.             }
  33.             printf("%c",(char) letter);
  34.         }
  35.         else
  36.         {
  37.             printf("%c",plaintext[i]);  
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement