Advertisement
LightningStalker

Simple Caesar cypher program

Sep 10th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. /*
  2. * Simple Caesar cypher algorithm
  3. * Preserves case and non-alpha characters
  4. * by The Lightning Stalker
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9.  
  10. #define ALPHA 26                    // letters in alphabet
  11. #define CONVBASE 7                  // conversion base
  12. #define COMPLEMENT ALPHA - CONVBASE // conversion complement
  13. #define CHARLMIN *"a" + CONVBASE    // smallest char without rollover
  14. #define CHARUMIN *"A" + CONVBASE    // . . .
  15.  
  16. static const char stringin[] = "Joyvtl Jvbuayf";  // string to convert
  17.  
  18. int main (int argc, char **argv)
  19. {
  20.     int c;
  21.     char stringout[256];
  22.    
  23.     for (c = 0; c < sizeof(stringin); c++)
  24.     {
  25.         if (isalpha(stringin[c]))
  26.             if (islower(stringin[c]))
  27.                 if (stringin[c] >= CHARLMIN)
  28.                     stringout[c] = stringin[c] - CONVBASE;
  29.                 else
  30.                     stringout[c] = stringin[c] + COMPLEMENT;
  31.             else
  32.                 if (stringin[c] >= CHARUMIN)
  33.                     stringout[c] = stringin[c] - CONVBASE;
  34.                 else
  35.                     stringout[c] = stringin[c] + COMPLEMENT;
  36.         else
  37.         stringout[c] = stringin[c];
  38.     }
  39.     printf("%s\n", stringout);
  40.     return(0);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement