Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
rot13.c File Reference

ROT13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. More...

#include <stdio.h>
#include <string.h>
#include <assert.h>
Include dependency graph for rot13.c:

Functions

void rot13 (char *s)
 for IO operations More...
 
static void test ()
 Self-test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

ROT13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet.

ROT13 transforms a piece of text by examining its alphabetic characters and replacing each one with the letter 13 places further along in the alphabet, wrapping back to the beginning if necessary. A becomes N, B becomes O, and so on up to M, which becomes Z, then the sequence continues at the beginning of the alphabet: N becomes A, O becomes B, and so on to Z, which becomes M.

Author
Jeremias Moreira Gomes

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
57 {
58 test(); // run self-test implementations
59 return 0;
60}
static void test()
Self-test implementations.
Definition: rot13.c:37
Here is the call graph for this function:

◆ rot13()

void rot13 ( char *  s)

for IO operations

for string operations for assert

Apply the ROT13 cipher

Parameters
scontains the string to be processed
23 {
24 for (int i = 0; s[i]; i++) {
25 if (s[i] >= 'A' && s[i] <= 'Z') {
26 s[i] = 'A' + ((s[i] - 'A' + 13) % 26);
27 } else if (s[i] >= 'a' && s[i] <= 'z') {
28 s[i] = 'a' + ((s[i] - 'a' + 13) % 26);
29 }
30 }
31}

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
37 {
38 char test_01[] = "The more I C, the less I see.";
39 rot13(test_01);
40 assert(strcmp(test_01, "Gur zber V P, gur yrff V frr.") == 0);
41
42 char test_02[] = "Which witch switched the Swiss wristwatches?";
43 rot13(test_02);
44 assert(strcmp(test_02, "Juvpu jvgpu fjvgpurq gur Fjvff jevfgjngpurf?") == 0);
45
46 char test_03[] = "Juvpu jvgpu fjvgpurq gur Fjvff jevfgjngpurf?";
47 rot13(test_03);
48 assert(strcmp(test_03, "Which witch switched the Swiss wristwatches?") == 0);
49
50 printf("All tests have successfully passed!\n");
51}
void rot13(char *s)
for IO operations
Definition: rot13.c:23
Here is the call graph for this function: