Advertisement
livingdub

Untitled

Feb 9th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. int main (int argc, string argv[])
  8. {    
  9.     //error if no key given
  10.     if (argc < 2)
  11.     {
  12.         printf("\nI'll need a key at the command prompt!\n\n");
  13.         return 1;
  14.     }
  15.    
  16.     //error if too much keys given
  17.     else if (argc > 2)
  18.     {
  19.         printf("\nThat's too much!\n\n");
  20.         return 1;
  21.     }
  22.    
  23.     //and for the right amount of arguments we can continue
  24.     else
  25.     {
  26.         string key = argv[1];
  27.        
  28.         //check for invalid chars in keyword
  29.         for (int i = 0, n = strlen(key); i < n; i++)
  30.         {
  31.             if (!(isalpha(key[i])))
  32.             {
  33.                 printf("\nI'll need a keyword, only letters!\n\n");
  34.                 return 1;
  35.             }
  36.         }
  37.        
  38.         //close the checkalpha iteration and ask for phrase to crypt
  39.         string plaintext = GetString();
  40.         int k = 0; //to correct for skipped plaintextchars
  41.        
  42.         //iterate through plaintext array we just got
  43.         for (int i = 0, n = strlen(plaintext); i < n; i++)
  44.         {
  45.             int j = i % strlen(key);
  46.  
  47.             //branch for all non-alphabetic chars
  48.             if (!(isalpha(plaintext[i])))
  49.             {
  50.                 printf("%c", plaintext[i]);
  51.                 k++;
  52.             }
  53.  
  54.             //branch for all alphabetic chars in plaintext
  55.             else
  56.             {
  57.                 //declare conversion constant for upper & lower case letters
  58.                 int a = 65;
  59.                 int b = 97;
  60.  
  61.                 //branch for all uppercase chars in plaintext
  62.                 if (isupper(plaintext[i]))
  63.                 {
  64.                     int convertedPlaintext = plaintext[i] - a;
  65.                    
  66.                     if (isupper(key[j]))
  67.                     {
  68.                         int convertedKey = key[j - k] - a;
  69.                         int result = (convertedPlaintext + convertedKey) % 26;
  70.                         printf("%c", result + a);
  71.                     }
  72.  
  73.                     else //islower
  74.                     {
  75.                         int convertedKey = key[j - k] - b;
  76.                         int result = (convertedPlaintext + convertedKey) % 26;
  77.                         printf("%c", result + a);
  78.                     }
  79.                 }
  80.                
  81.                 //branch for all lower case chars in OGPhrase
  82.                 else
  83.                 {
  84.                     int convertedPlaintext = plaintext[i] - b;
  85.  
  86.                     if (isupper(key[j]))
  87.                     {
  88.                         int convertedKey = key[j - k] - a; //is a still 65 here scopewise?
  89.                         int result = (convertedPlaintext + convertedKey) % 26;
  90.                         printf("%c", result + b);
  91.                     }
  92.  
  93.                     else
  94.                     {
  95.                         int convertedKey = key[j - k] - b;
  96.                         int result = (convertedPlaintext + convertedKey) % 26;
  97.                         printf("%c", result + b);
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.         printf("\n");
  103.         return 0;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement