summaryrefslogtreecommitdiffstats
path: root/4/13.c
blob: 28918c573ab5678714b76316acc6c9852c96b000 (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
#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(sizeof(char) * MAXLEN);

  fgets(s, MAXLEN, 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];
  }
}