在Linux中没有可用的strrev()函数吗?

时间:2021-08-02 17:29:13

I tried to write code using strrev(). I included <string.h> but still I'm getting an "undefined reference to strrev" error.

我尝试使用strrev()来编写代码。我包括 <字符串。h> ,但我仍然得到一个“未定义的对strrev的引用”错误。

I found that strrev() doesn't have man page at all. Why?

我发现strrev()根本没有人页。为什么?

Doesn't Linux support strrev()?

Linux不支持strrev()?

5 个解决方案

#1


27  

Correct. Use one of the alternative implementations available:

正确的。使用一个可用的替代实现:

#include <string.h>

char *strrev(char *str)
{
      char *p1, *p2;

      if (! str || ! *str)
            return str;
      for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
      {
            *p1 ^= *p2;
            *p2 ^= *p1;
            *p1 ^= *p2;
      }
      return str;
}

#2


16  

#include <string.h>

char *strrev(char *str)
{
    int i = strlen(str) - 1, j = 0;

    char ch;
    while (i > j)
    {
        ch = str[i];
        str[i] = str[j];
        str[j] = ch;
        i--;
        j++;
    }
    return str;
}

#3


2  

Unfortunately, strrev seems to be absent from glibc's string.h.

不幸的是,strrev似乎没有出现在glibc的字符串中。

Obviously, I'm late to the here's-some-code party, but I like this implementation.

显然,我在这里已经晚了,但是我喜欢这个实现。

#define MAX_CHARS 10000
// safe_usub -- perform safe unsigned subtraction
size_t safe_usub (size_t x, size_t y) {
  return x > y ? x - y : y - x ;
}

char* str_reverse (const char* const str) {
  if (!str) { return NULL; }

  size_t len = strnlen(str, MAX_CHARS);
  char*  new = malloc( sizeof(char) * len );

  size_t i;
  for (i = 0; i < len; i++) {
    new[i] = str[ safe_usub(i + 1, len) ];
  }

  new[i] = 0;

  return new;
}

#4


1  

There is no string library function "For reverse the string"

没有字符串库函数“用于反转字符串”

strrev() is not present in GCC Compiler in Linux or Ubuntu

strrev()不存在于Linux或Ubuntu中的GCC编译器中。

#5


0  

There Is No Any String Library Function "For Reverse The String"

没有任何字符串库函数“用于反转字符串”

strrev() Is Not Present in GCC Compiler In Linux Or Ubuntu.............

strrev()不存在于Linux或Ubuntu中的GCC编译器中。

reverse.c:

reverse.c:

//////MAKE YOUR OWN REVERSE FUNCTION////





/*
  * C Program to Reverse the String using Recursion
 */
#include <stdio.h>
#include <string.h>

   void reverse(char [], int, int);
       int main()
 {
       char str1[20];
        int size;

         printf("Enter a string to reverse: ");
           scanf("%s", str1);
               size = strlen(str1);
                    reverse(str1, 0, size - 1);
             printf("The string after reversing is: %s\n", str1);
                             return 0;
    }

 void reverse(char str1[], int index, int size)
{
    char temp;
     temp = str1[index];
        str1[index] = str1[size - index];
        str1[size - index] = temp;
    if (index == size / 2)`    `
    {
        return;`enter code here`
    }
        reverse(str1, index + 1, size);
}

#1


27  

Correct. Use one of the alternative implementations available:

正确的。使用一个可用的替代实现:

#include <string.h>

char *strrev(char *str)
{
      char *p1, *p2;

      if (! str || ! *str)
            return str;
      for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
      {
            *p1 ^= *p2;
            *p2 ^= *p1;
            *p1 ^= *p2;
      }
      return str;
}

#2


16  

#include <string.h>

char *strrev(char *str)
{
    int i = strlen(str) - 1, j = 0;

    char ch;
    while (i > j)
    {
        ch = str[i];
        str[i] = str[j];
        str[j] = ch;
        i--;
        j++;
    }
    return str;
}

#3


2  

Unfortunately, strrev seems to be absent from glibc's string.h.

不幸的是,strrev似乎没有出现在glibc的字符串中。

Obviously, I'm late to the here's-some-code party, but I like this implementation.

显然,我在这里已经晚了,但是我喜欢这个实现。

#define MAX_CHARS 10000
// safe_usub -- perform safe unsigned subtraction
size_t safe_usub (size_t x, size_t y) {
  return x > y ? x - y : y - x ;
}

char* str_reverse (const char* const str) {
  if (!str) { return NULL; }

  size_t len = strnlen(str, MAX_CHARS);
  char*  new = malloc( sizeof(char) * len );

  size_t i;
  for (i = 0; i < len; i++) {
    new[i] = str[ safe_usub(i + 1, len) ];
  }

  new[i] = 0;

  return new;
}

#4


1  

There is no string library function "For reverse the string"

没有字符串库函数“用于反转字符串”

strrev() is not present in GCC Compiler in Linux or Ubuntu

strrev()不存在于Linux或Ubuntu中的GCC编译器中。

#5


0  

There Is No Any String Library Function "For Reverse The String"

没有任何字符串库函数“用于反转字符串”

strrev() Is Not Present in GCC Compiler In Linux Or Ubuntu.............

strrev()不存在于Linux或Ubuntu中的GCC编译器中。

reverse.c:

reverse.c:

//////MAKE YOUR OWN REVERSE FUNCTION////





/*
  * C Program to Reverse the String using Recursion
 */
#include <stdio.h>
#include <string.h>

   void reverse(char [], int, int);
       int main()
 {
       char str1[20];
        int size;

         printf("Enter a string to reverse: ");
           scanf("%s", str1);
               size = strlen(str1);
                    reverse(str1, 0, size - 1);
             printf("The string after reversing is: %s\n", str1);
                             return 0;
    }

 void reverse(char str1[], int index, int size)
{
    char temp;
     temp = str1[index];
        str1[index] = str1[size - index];
        str1[size - index] = temp;
    if (index == size / 2)`    `
    {
        return;`enter code here`
    }
        reverse(str1, index + 1, size);
}