SHARE
TWEET

nen thingy

a guest Dec 29th, 2018 12 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void assertEquals(char*, char*);
  5. char* reverse(char*);
  6. char* reverse_helper(char* s, int, int);
  7.  
  8. // clang -o out main.c && ./out
  9. int main() {
  10.   assertEquals("", reverse(""));
  11.   assertEquals("a", reverse("a"));
  12.  
  13.   char in[] = "ab";
  14.   char out[] = "ba";
  15.   assertEquals(out, reverse(in));
  16.  
  17.   char in2[] = "cba";
  18.   char out2[] = "abc";
  19.   assertEquals(out2, reverse(in2));
  20.  
  21.   return 0;
  22. }
  23.  
  24. char* reverse(char* s) {
  25.   return reverse_helper(s, 0, strlen(s) - 1);
  26. }
  27.  
  28. char* reverse_helper(char* s, int curr, int end) {
  29.   if (curr == end || end < curr) {
  30.     return s;
  31.   }
  32.  
  33.   char temp = s[curr];
  34.   s[curr] = s[end];
  35.   s[end] = temp;
  36.   return reverse_helper(s, curr + 1, end - 1);
  37. }
  38.  
  39. void assertEquals(char* s1, char* s2) {
  40.   if (strcmp(s1, s2) != 0) {
  41.     printf("FAIL: Expected '%s', Actual '%s'\n", s1, s2);
  42.   } else {
  43.     printf("SUCC: Expected '%s', Actual '%s'\n", s1, s2);
  44.   }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top