Reversing a string in C using pointers?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 17:15:03

问题


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!


回答1:


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).



回答2:


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



回答3:


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 ;
}



回答4:


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;
}


来源:https://stackoverflow.com/questions/7544387/reversing-a-string-in-c-using-pointers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!