Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Simple Caesar cypher algorithm
- * Preserves case and non-alpha characters
- * by The Lightning Stalker
- */
- #include <stdio.h>
- #include <ctype.h>
- #define ALPHA 26 // letters in alphabet
- #define CONVBASE -7 // conversion base
- #if CONVBASE < 0
- #define CONVBASE_IS_NEG
- #endif
- #ifdef CONVBASE_IS_NEG
- #define COMPLEMENT ALPHA + CONVBASE // conversion complement
- #define CHARLMIN *"a" - CONVBASE // smallest char without rollover
- #define CHARUMIN *"A" - CONVBASE // . . .
- #else
- #define COMPLEMENT CONVBASE - ALPHA // conversion complement
- #define CHARLMIN *"z" - CONVBASE // largest character without
- #define CHARUMIN *"Z" - CONVBASE // rollover
- #endif
- static const char stringin[] = "Joyvtl Jvbuayf"; // string to convert
- //~ static const char stringin[] = "Chrome Country"; // . . .
- int main (int argc, char **argv)
- {
- int c;
- char stringout[256];
- for (c = 0; c < sizeof(stringin); c++) // Take a look at each
- { // character, one at a time
- if (isalpha(stringin[c])) // and apply the cypher.
- if (islower(stringin[c]))
- #ifdef CONVBASE_IS_NEG
- if (stringin[c] >= CHARLMIN)
- #else
- if (stringin[c] <= CHARLMIN)
- #endif
- stringout[c] = stringin[c] + CONVBASE;
- else
- stringout[c] = stringin[c] + COMPLEMENT;
- else
- #ifdef CONVBASE_IS_NEG
- if (stringin[c] >= CHARUMIN)
- #else
- if (stringin[c] <= CHARUMIN)
- #endif
- stringout[c] = stringin[c] + CONVBASE;
- else
- stringout[c] = stringin[c] + COMPLEMENT;
- else
- stringout[c] = stringin[c];
- }
- printf("%s\n", stringout);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement