-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path4-13.c
53 lines (43 loc) · 989 Bytes
/
4-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
/*
* Exercise 4-13. Write a recursive version of the function reverse(s), which
* reverses the string s in place.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
/* functions */
void reverse(char s[]);
void reverse_inplace(char str[], int begin, int end);
void swap(char arr[], size_t i, size_t j);
/* reverse: interface function to reverse_inplace */
void reverse(char s[])
{
reverse_inplace(s, 0, strlen(s) - 1);
}
/* reverse_inplace: reverse string s in place, recursive version */
void reverse_inplace(char str[], int begin, int end)
{
if (begin > end) /* exit condition */
return;
swap(str, begin, end);
reverse_inplace(str, ++begin, --end);
}
/* swap: interchange v[i] and v[j] */
void swap(char v[], size_t i, size_t j)
{
char tmp;
tmp = v[i];
v[i] = v[j];
v[j] = tmp;
}
int main(void)
{
char str[MAXLEN];
printf("Enter a string to reverse: ");
scanf("%s", str);
reverse(str);
printf("%s\n", str);
return 0;
}