0

I have to write a function that recursively returns a string from the end. The following 3 functions are available for us:

#include <stdio.h>
#include <stdlib.h>

int length(char* s) {
    int n = 0;
    while (*s != '\0') {
        n++;
        s++;
    }

    return n;
}


void copy(char* s, int n, char* t) {
    int i = 0;
    while (i < n) {
        t[i] = s[i];
        i++;
    }
}

char* putBack(char* s, char c) {
    const int n = length(s);
    char* r = malloc(sizeof(char) * (n + 2));
    copy(s, n, r);
    r[n] = c;
    r[n + 1] = '\0';
    return r;
}

My implemented recursive method is:

char* reverseRec(char* s) {
    char* pointer = s;
    char* string;
    if (*pointer == '\0') { 
        return pointer--;
    }
    else{
        string = reverseRec(pointer + 1);
        string = putBack(string, *pointer);
        return string;
    }
}

For example, if I have "call" as input, then "llac" is the output. So, the method should work. My question now is how can I clean up the reserved space of putBack() (there is a malloc) with free() after each call?

Thanks for helping! :)

oOBeenOo
  • 1
  • 1
  • Firstly you will have to have `reverseRec` return something when `*pointer == '\0'` to avoid *undefined behavior* for using "return value" of non-void function whose execution is reached at the end without executing `return` statement. – MikeCAT Apr 15 '21 at 18:24
  • 1
    It doesn't look like the intended functionality needs dynamic allocation at all. – Eugene Sh. Apr 15 '21 at 18:25
  • Having the OS do the cleanup will be the easiest way. [c - What REALLY happens when you don't free after malloc? - Stack Overflow](https://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc) – MikeCAT Apr 15 '21 at 18:25
  • 1
    @MikeCAT I don't think it is a good suggestion in this context. – Eugene Sh. Apr 15 '21 at 18:26
  • @EugeneSh. OP may choose not to use `malloc()` nor `putBack()` in the `*pointer == '\0'` case. Then this will be safe way to avoid passing `free()` a pointer not allocated via `malloc()` family. – MikeCAT Apr 15 '21 at 18:28
  • I think it's important to realize that reversing a string takes the same number of bytes as itself. Even if you use `malloc`, it should be used just once outside of the recursion. – babon Apr 15 '21 at 18:35
  • @VladfromMoscow Its my second code with C... – oOBeenOo Apr 15 '21 at 18:39
  • @oOBeenOo Why are not you using realloc instead of malloc? – Vlad from Moscow Apr 15 '21 at 18:51
  • @VladfromMoscow Because I am not allowed to change any of the functions for the exercise. I just need to implement the recursive function. – oOBeenOo Apr 15 '21 at 18:58
  • Do you *have to* use `putBack`? – Emanuel P Apr 15 '21 at 19:30
  • @EmanuelP Yes. A tip from my tutor was the use of free in the recursive method. – oOBeenOo Apr 15 '21 at 19:53
  • @oOBeenOo Sorry, to explain that. You store first char temporarily, then swap with the last, set the last to 0 so it becomes the end, do the recursive call starting at the next char, then restor the last char with the stored temporary first char. Put simple: you can swap the first and last going to the center. There's no need for allocation. – Emanuel P Apr 15 '21 at 20:05

0 Answers0