| 1 | #include <stdio.h> |
| 2 | #include <ctype.h> |
| 3 | |
| 4 | // FIXME: Comment the code !!! |
| 5 | |
| 6 | void swap(char* v1, char* v2) { |
| 7 | if (v1 != v2) { |
| 8 | char tmp = *v1; |
| 9 | *v1 = *v2; |
| 10 | *v2 = tmp; |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | int stringLength(const char* str) { |
| 15 | int length; |
| 16 | |
| 17 | for(length = 0; str[length] != '\0'; ++length); |
| 18 | return length; |
| 19 | } |
| 20 | |
| 21 | // FIXME: this function have issues with non english characters |
| 22 | void reverseString(char* str) { |
| 23 | int length = stringLength(str); |
| 24 | |
| 25 | for (int i = length - 1; i >= length/2; --i) { |
| 26 | swap(&str[i], &str[(length - 1) - i]); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // FIXME: this function have issues with non english characters |
| 31 | void permAlphaChar(char* str, int key) { |
| 32 | char alphabet[26] = "abcdefghijklmnopqrstuvwxyz"; |
| 33 | int str_length = stringLength(str); |
| 34 | |
| 35 | for (int i = 0; i < str_length; i++) { |
| 36 | //if (str[i] == ' ') continue; |
| 37 | for (int j = 0; j < 26; j++) { |
| 38 | if (str[i] == alphabet[j]) { |
| 39 | str[i] = alphabet[(j+key) % 26]; |
| 40 | break; |
| 41 | } else if (str[i] == toupper(alphabet[j])) { |
| 42 | str[i] = toupper(alphabet[(j+key) % 26]); |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int main() { |
| 50 | char rev_msg[] = "Bonjour le monde"; |
| 51 | int rev_length = stringLength(rev_msg); |
| 52 | char perm_msg[] = "Bonjour a tous et toutes"; |
| 53 | int perm_length = stringLength(perm_msg); |
| 54 | |
| 55 | printf("La chaine de caracteres a inverser est \"%s\" et a pour longueur %d caractere(s)\n", rev_msg, rev_length); |
| 56 | reverseString(rev_msg); |
| 57 | printf("La chaine inversee de caracteres est \"%s\"\n", rev_msg); |
| 58 | |
| 59 | printf("\n"); |
| 60 | |
| 61 | printf("La chaine de caracteres a permuter est \"%s\" et a pour longueur %d caractere(s)\n", perm_msg, perm_length); |
| 62 | permAlphaChar(perm_msg, 13); |
| 63 | printf("La chaine permutee de caracteres est \"%s\"\n", perm_msg); |
| 64 | |
| 65 | return 0; |
| 66 | } |