Rot13 in C

Published on 28 April 2022 (Updated: 18 September 2022)

Rot13 in C

Welcome to the Rot13 in C page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

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

void rot13(char *str) {
    for(int i = 0; str[i] != '\0'; i++) {
        char c = *(str + i);
        if(('a' <= c && 'n' > c) || ('A' <= c && 'N' > c)) {
            *(str + i) += 13;
        } else if(('n' <= c && 'z' >= c) || ('N' <= c && 'Z' >= c)) {
            *(str + i) -= 13;
        }
    }
}

int main(int argc, char *argv[]) {
    if(argc == 2 && strlen(argv[1]) != 0 && !isdigit(*argv[1])) {
        rot13(argv[1]);
        printf("%s\n", argv[1]);
    } else {
        printf("Usage: please provide a string to encrypt\n");
    }
    
    return 0;
}

Rot13 in C was written by:

If you see anything you'd like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 09 2019 20:06:02. The solution was first committed on Oct 09 2019 18:23:24. As a result, documentation below may be outdated.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.