C Program to Perform ROT47 Cipher


The ROT47 Cipher can be implemented in the following C/C++ Function. The ROT47 Cipher helps to encode/decode plain-text.

#include <stdio.h>
#include <string.h>

void rot47(char *buf, int l) {
    for (int i = 0; i < l; ++ i) {
        if (buf[i] >= 33 && buf[i] <= 126) {
            buf[i] = 33 + ((buf[i] + 14) % 94);
        }
    }
}

int main(int argc, char* argv[]) {
    if (argc == 0) {
        return 0;
    }
    for (int i = 1; i < argc; ++ i) {
        rot47(argv[i], strlen(argv[i]));
        printf("%s\n", argv[i]);
    }
    return 0;
}

We can compile using gcc compiler:

$ ./gcc rot47.c -o rot47

Then, we can perform the ROT47 Cipher on the command line parameters:

$ ./rot47 "Hello, World" "How Are You!"
w6==@[ (@C=5
w@H pC6 *@FP

The same text ROT47-ed twice will revert to original text.

$ ./rot47 "w6==@[ (@C=5" "w@H pC6 *@FP"
Hello, World
How Are You!

–EOF (The Ultimate Computing & Technology Blog) —

208 words
Last Post: Teaching Kids Programming - Sum of Unique Elements
Next Post: Teaching Kids Programming - Maximum Number of Words You Can Type

The Permanent URL is: C Program to Perform ROT47 Cipher (AMP Version)

Leave a Reply