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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
}
#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:

1
$ ./gcc rot47.c -o rot47
$ ./gcc rot47.c -o rot47

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

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

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

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

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
225 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

Leave a Reply