-
Notifications
You must be signed in to change notification settings - Fork 1
/
13.c
54 lines (45 loc) · 1.25 KB
/
13.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <string.h>
/**
* @brief Reverses a string s in place.
*
* @param s The string to be reversed.
*/
void reverse (char s[])
{
static int begin = 0; /* Index of start of the string s. */
static int end = 1; /* Index of end of the string s. */
/* If we are in the first run of this function */
if (begin == 0) {
end = strlen(s) - 1; /* Compute string size, excluding null character */
}
/* If begin overlap end, we reached base case. */
if (begin >= end) {
/* Resetting static variables */
begin = 0;
end = 1;
} else {
int _c = 0; /* Temporary variable to perform the swap. */
/* Swap values */
_c = s[begin];
s[begin++] = s[end];
s[end--] = _c;
/* Call reverse recursively */
reverse(s);
}
}
main()
{
char first[] = "Socorram-me, subi no onibus em Marrocos!";
char second[] = "ovo";
char third[] = "Lorem ipsum";
printf("[*]\"%s\" =>\n", first);
reverse(first);
printf("\t\t\"%s\"\n\n", first);
printf("[*]\"%s\" =>\n", second);
reverse(second);
printf("\t\t\"%s\"\n\n", second);
printf("[*]\"%s\" =>\n", third);
reverse(third);
printf("\t\t\"%s\"\n\n", third);
}