Commit | Line | Data |
---|---|---|
c2bc2b96 JB |
1 | #include <stdio.h> |
2 | ||
3 | int stringLength(const char* str) { | |
4 | int result = 0; | |
5 | ||
6 | while (str[result] != '\0') { | |
7 | ++result; | |
8 | } | |
9 | ||
10 | return result; | |
11 | } | |
12 | ||
13 | void swap(char* v1, char* v2) { | |
14 | char temp = *v1; | |
15 | *v1 = *v2; | |
16 | *v2 = temp; | |
17 | } | |
18 | ||
19 | void reverseString(char* str) { | |
20 | int length = stringLength(str); | |
21 | int halfLength = length / 2; | |
22 | ||
23 | for (int charId = 0; charId < halfLength; ++charId) { | |
24 | swap(&str[charId], &str[length - charId - 1]); | |
25 | } | |
26 | } | |
27 | ||
28 | /** Perform a classical ROT13 permutation in-place */ | |
29 | void rot13(char* str) { | |
30 | int length = stringLength(str); | |
31 | ||
32 | for (int charId = 0; charId < length; ++charId) { | |
33 | char chrValue = str[charId]; | |
34 | ||
35 | if (chrValue >= 'a' && chrValue <= 'z') { | |
36 | str[charId] = | |
37 | (chrValue - 'a' // Translate chrValue from 'a'-'z' to 0-25 | |
38 | + 13) // Add 13 | |
39 | % 26 // Rotate in 0-25 | |
40 | + 'a'; // Translate the value from 0-25 to 'a'-'z' | |
41 | } else if (chrValue >= 'A' && chrValue <= 'Z') { | |
42 | str[charId] = (chrValue - 'A' + 13) % 26 + 'A'; | |
43 | } | |
44 | } | |
45 | } | |
46 | ||
47 | int main() { | |
48 | char msg[] = "Votre message"; | |
49 | printf("Initial value: \"%s\"\n", msg); | |
50 | reverseString(msg); | |
51 | printf("Reversed : \"%s\"\n", msg); | |
52 | reverseString(msg); // Restore the original message | |
53 | rot13(msg); | |
54 | printf("ROT13 : \"%s\"\n", msg); | |
55 | } |