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 | int main() { | |
29 | char msg[] = "Votre message"; | |
30 | printf("Initial value: \"%s\"\n", msg); | |
31 | reverseString(msg); | |
32 | printf("Reversed : \"%s\"\n", msg); | |
33 | } |