aboutsummaryrefslogtreecommitdiff
path: root/reverse_string.c
blob: c94403f8c042be7d4fdc3d1fe72e04a5259770a5 (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
// 344. Reverse String
#include "leetcode.h"

/*
 * write a function that reverses a string. the input string is given as an
 * array of characters 's'. you must do this by modifying teh input array in
 * place with 'O(1)' extra memroy
 */

void reverseString(char *s, int sSize) {
  char *ptr = s + sSize - 1, tmp = *s;
  if (sSize <= 1)
    return;
  *s = *ptr;
  *ptr = tmp;
  reverseString(s + 1, sSize - 2);
}

int main() {
  char *s1 = "hello", *s2 = "Hannah";
  reverseString(s1, strlen(s1));
  reverseString(s2, strlen(s2));
  printf("%s\n", s1); // expect: olleh
  printf("%s\n", s2); // expect: hannaH
}