【问题标题】:How to reverse a string without using in-built and temporary variables [duplicate]如何在不使用内置变量和临时变量的情况下反转字符串[重复]
【发布时间】:2014-04-14 11:59:48
【问题描述】:

没有临时变量和字符串反转等内置函数的反向操作。

【问题讨论】:

  • with out temporary variable 实际上我不认为这样的解决方案存在..
  • 你应该让我们知道你为什么会有这些限制,否则我们可能会怀疑我们只是在为你做家庭作业
  • 不是这样的,我遇到过这样的情况,这可以给我的项目一个解决方案。
  • 最好详细说明“没有临时人员”的确切含义。如果没有漏洞可以利用,那可能是不可能的。

标签: c


【解决方案1】:

您可以像这样使用 XOR 逻辑来做到这一点:

char* rev(char* str)
{
    int end = strlen(str) - 1;
    int start = 0;

    while (start < end)
    {
        str[start] ^= str[end];
        str[end] ^= str[start];
        str[start] ^= str[end];

        ++start;
        --end;
    }

    return str;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    • 2022-06-14
    • 1970-01-01
    • 2019-05-21
    • 2012-11-29
    相关资源
    最近更新 更多