From eb0c99634dc36a57993cb69a55985d37c2b24af3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 19 Feb 2017 00:49:59 +0100 Subject: [PATCH] exo3.c: implement ROTX where X the number of latin alphabet letter to rotate. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit And make a use of it for X=13. There's still a lot of rooms for improvements but it works. Signed-off-by: Jérôme Benoit --- exo3/exo3.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/exo3/exo3.c b/exo3/exo3.c index d1b0956..89170ea 100644 --- a/exo3/exo3.c +++ b/exo3/exo3.c @@ -1,4 +1,5 @@ #include +#include //FIXME: Comment the code !!! @@ -26,13 +27,40 @@ void reverseString(char* str) { } } +// FIXME: this function have issues with non english characters +void permAlphaChar(char* str, int key) { + char alphabet[26] = "abcdefghijklmnopqrstuvwxyz"; + int str_length = stringLength(str); + + for (int i; i < str_length; i++) { + //if (str[i] == ' ') continue; + for (int j = 0; j < 26; j++) { + if (str[i] == alphabet[j]) { + str[i] = alphabet[(j+key) % 26]; + break; + } else if (str[i] == toupper(alphabet[j])) { + str[i] = toupper(alphabet[(j+key) % 26]); + break; + } + } + } +} + int main() { - char msg[] = "Bonjour et a bientot"; - int length = stringLength(msg); + char rev_msg[] = "Bonjour le monde"; + int rev_length = stringLength(rev_msg); + char perm_msg[] = "Bonjour a tous et toutes"; + int perm_length = stringLength(perm_msg); + + printf("La chaine de caracteres a inverser est \"%s\" et a pour longueur %d caractere(s)\n", rev_msg, rev_length); + reverseString(rev_msg); + printf("La chaine inversee de caracteres est \"%s\"\n", rev_msg); - printf("La chaine de caracteres est \"%s\" et a pour longueur %d caractere(s)\n", msg, length); - reverseString(msg); - printf("La chaine inversee de caracteres est \"%s\"\n", msg); + printf("\n"); + + printf("La chaine de caracteres a permuter est \"%s\" et a pour longueur %d caractere(s)\n", perm_msg, perm_length); + permAlphaChar(perm_msg, 13); + printf("La chaine permutee de caracteres est \"%s\"\n", perm_msg); return 0; } -- 2.34.1