4

Language: C

I am trying to program a C function which uses the header char *strrev2(const char *string) as part of interview preparation, the closest (working) solution is below, however I would like an implementation which does not include malloc... Is this possible? As it returns a character meaning if I use malloc, a free would have to be used within another function.

char *strrev2(const char *string){
    int l=strlen(string);
    char *r=malloc(l+1);
    for(int j=0;j<l;j++){
        r[j] = string[l-j-1];
    }
    r[l] = '\0';
    return r;
}

[EDIT] I have already written implementations using a buffer and without the char. Thanks tho!

Edward Yang
  • 41
  • 1
  • 3
  • You could free `string`, if it is no longer needed, which would leave you with having to free just one thing. However, freeing it here is considered very bad programming, as it is an unexpected side effect. – Eran Zimmerman Gonen Sep 25 '11 at 08:19
  • You could also use a static char **, which points to a pointer to a memory area which you allocate on the first call and realloc as needed on subsequent ones. I wouldn't use this approach because you won't know when the last call is so you can't free the memory, but it might impress them in the interview. :) – Uffe Sep 25 '11 at 08:58

4 Answers4

8

No - you need a malloc.

Other options are:

  • Modify the string in-place, but since you have a const char * and you aren't allowed to change the function signature, this is not possible here.
  • Add a parameter so that the user provides a buffer into which the result is written, but again this is not possible without changing the signature (or using globals, which is a really bad idea).
Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3

You may do it this way and let the caller responsible for freeing the memory. Or you can allow the caller to pass in an allocated char buffer, thus the allocation and the free are all done by caller:

void strrev2(const char *string, char* output)
{
    // place the reversed string onto 'output' here
}

For caller:

char buffer[100];
char *input = "Hello World";
strrev2(input, buffer);
// the reversed string now in buffer
Gant
  • 29,661
  • 6
  • 46
  • 65
  • 1
    If you want the caller to be responsible for deallocating the memory, the best way to make sure they remember to do so is to have them do the allocation. – Steve Rowe Sep 26 '11 at 07:34
0

You could use a static char[1024]; (1024 is an example size), store all strings used in this buffer and return the memory address which contains each string. The following code snippet may contain bugs but will probably give you the idea.

#include <stdio.h>
#include <string.h>

char* strrev2(const char* str)
{

    static char buffer[1024];
    static int  last_access; //Points to leftmost available byte;

    //Check if buffer has enough place to store the new string
    if( strlen(str) <= (1024 - last_access) )
    {
        char* return_address = &(buffer[last_access]);
        int i;

        //FixMe - Make me faster
        for( i = 0; i < strlen(str) ; ++i )
        {
            buffer[last_access++] = str[strlen(str) - 1 - i];
        }       

        buffer[last_access] = 0;
        ++last_access;

        return return_address;           
    }else
    {
        return 0;
    }
}

int main()
{
    char* test1 = "This is a test String";
    char* test2 = "George!";
    puts(strrev2(test1));
    puts(strrev2(test2));
    return 0 ;
}
0

reverse string in place

char *reverse (char *str)
{
  register char c, *begin, *end;
  begin = end = str;

  while (*end != '\0') end ++;

  while (begin < --end) 
  {
    c = *begin;
    *begin++ = *end;
    *end = c;
  }
  return str;
}