summaryrefslogtreecommitdiffstats
path: root/4/13.c
blob: cc359aa9dfe9a02a3fef10767fd59c08748f6df2 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 1000

/* Reverses string in place using recursion */
void reverse(char[], int, int);

int main(int argc, char *argv[]) {
  char *s;
  size_t n;

  s = malloc(MAXLEN);
  n = sizeof(s);

  getline(&s, &n, stdin);
  reverse(s, 0, strlen(s) - 2);
  printf("\t%s\n", s);

  free(s);
  return 0;
}

void reverse(char s[], int i, int j) {
  if (i < j) {
    reverse(s, i + 1, j - 1);
    s[i] = s[i] ^ s[j];
    s[j] = s[i] ^ s[j];
    s[i] = s[i] ^ s[j];
  }
}