fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char* string_reverse_aux(char* s1, char* s2, size_t count, size_t length) {
  6. if (count == length) {
  7. s2[count] = '\0';
  8. return s2;
  9. } else {
  10. s2[count] = s1[length - 1 - count];
  11. return string_reverse_aux(s1, s2, count + 1, length);
  12. }
  13. }
  14.  
  15. char* string_reverse(char* s) {
  16. int n = strlen(s);
  17. char* _s = (char*)malloc(sizeof(char) * (n + 1));
  18. return string_reverse_aux(s, _s, 0, n);
  19. }
  20.  
  21. int main(void) {
  22. char* s = "abcdefgh";
  23. printf("%s\n%s\n", s, string_reverse(s));
  24. return EXIT_SUCCESS;
  25. }
  26.  
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
abcdefgh
hgfedcba