View difference between Paste ID: qdxDzCNL and hSpQBMtz
SHOW: | | - or go back to the newest paste.
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
11+
#define CONVBASE -7                 // conversion base
12-
#define COMPLEMENT ALPHA - CONVBASE // conversion complement
12+
#if CONVBASE < 0
13-
#define CHARLMIN *"a" + CONVBASE    // smallest char without rollover
13+
#define CONVBASE_IS_NEG
14-
#define CHARUMIN *"A" + CONVBASE    // . . .
14+
#endif
15
#ifdef CONVBASE_IS_NEG
16
#define COMPLEMENT ALPHA + CONVBASE // conversion complement
17
#define CHARLMIN *"a" - CONVBASE    // smallest char without rollover
18
#define CHARUMIN *"A" - CONVBASE    // . . .
19
#else
20
#define COMPLEMENT CONVBASE - ALPHA // conversion complement
21
#define CHARLMIN *"z" - CONVBASE    // largest character without
22
#define CHARUMIN *"Z" - CONVBASE    // rollover
23-
    for (c = 0; c < sizeof(stringin); c++)
23+
#endif
24-
    {
24+
25-
        if (isalpha(stringin[c]))
25+
26
//~ static const char stringin[] = "Chrome Country";  // . . .
27
28-
                    stringout[c] = stringin[c] - CONVBASE;
28+
29
{
30
    int c;
31
    char stringout[256];
32
    
33-
                    stringout[c] = stringin[c] - CONVBASE;
33+
    for (c = 0; c < sizeof(stringin); c++)   // Take a look at each
34
    {                                        // character, one at a time
35
        if (isalpha(stringin[c]))            // and apply the cypher.
36
            if (islower(stringin[c]))
37
#ifdef CONVBASE_IS_NEG
38
                if (stringin[c] >= CHARLMIN)
39
#else
40
                if (stringin[c] <= CHARLMIN)
41
#endif
42
                    stringout[c] = stringin[c] + CONVBASE;
43
                else
44
                    stringout[c] = stringin[c] + COMPLEMENT;
45
            else
46
#ifdef CONVBASE_IS_NEG
47
                if (stringin[c] >= CHARUMIN)
48
#else
49
                if (stringin[c] <= CHARUMIN)
50
#endif
51
                    stringout[c] = stringin[c] + CONVBASE;
52
                else
53
                    stringout[c] = stringin[c] + COMPLEMENT;
54
        else
55
        stringout[c] = stringin[c];
56
    }
57
    printf("%s\n", stringout);
58
    return(0);
59
}