使用指针反转 C 中的字符串?

发布于 2024-12-06 09:11:11 字数 442 浏览 0 评论 0原文

语言:C

我正在尝试编写一个 C 函数,该函数使用标头 char *strrev2(const char *string) 作为面试准备的一部分,最接近的(工作)解决方案如下,但是我想要一个不包含 malloc 的实现……这可能吗?因为它返回一个字符,这意味着如果我使用 malloc,则必须在另一个函数中使用 free。

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

[编辑] 我已经使用缓冲区而不使用字符编写了实现。谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傾城如夢未必闌珊 2024-12-13 09:11:12

不 - 你需要一个 malloc。

其他选项有:

  • 修改字符串 -place,但由于您有一个 const char * 并且不允许您更改函数签名,因此这里不可能这样做。
  • 添加一个参数,以便用户提供一个将结果写入其中的缓冲区,但同样,如果不更改签名(或使用全局变量,这是一个非常糟糕的主意),这是不可能的。

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).
多情出卖 2024-12-13 09:11:12

您可以这样做,并让调用者负责释放内存。或者你可以允许调用者传入一个已分配的字符缓冲区,这样分配和释放都由调用者完成:

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

对于调用者:

char buffer[100];
char *input = "Hello World";
strrev2(input, buffer);
// the reversed string now in buffer

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
无力看清 2024-12-13 09:11:12

您可以使用static char[1024];(1024 是示例大小),存储此缓冲区中使用的所有字符串并返回包含每个字符串的内存地址。以下代码片段可能包含错误,但可能会给您带来想法。

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

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 ;
}
陌若浮生 2024-12-13 09:11:12

将字符串反转到位

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文