0

I am unable to use strrev() function even after including string.h in Ubuntu 20.04. Compiler says undefined reference to strrev(), but strlen() and other functions work. What should I do?

Anurag Pathak
  • 15
  • 1
  • 9
  • Since you are learning C++, it has a powerful [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) class. Consider using it. Be aware that [UTF-8 is used everywhere](http://utf8everywhere.org/) in 2021. If you compile with [GCC](http://gcc.gnu.org/) invoke it with all warnings and debug info: so `gcc -Wall -Wextra -g` for C code, and `g++ -Wall -Wextra -g ` for C++ code. BTW you could be interested by [RefPerSys](http://refpersys.org/). Then send me an email to – Basile Starynkevitch Feb 14 '21 at 08:47
  • Don't forget to use the [GDB](https://www.gnu.org/software/gdb/) debugger, or [strace(1)](https://man7.org/linux/man-pages/man1/strace.1.html) to understand the behavior of programs on Linux – Basile Starynkevitch Feb 14 '21 at 08:52
  • 1
    The profile of Anurag Pathak is mentioning C++; my comment was based on that public profile (which does *not* mention C) – Basile Starynkevitch Feb 14 '21 at 08:55
  • `strrev()` is not a C standard library function – qrdl Feb 14 '21 at 11:26

1 Answers1

1

You need to implement it yourself.

char *strrev(char *str)
{
    char *end, *wrk = str;
    {
        if(str && *str)
        {
            end = str + strlen(str) - 1;
            while(end > wrk)
            {
                char temp;

                temp = *wrk;
                *wrk++ = *end;
                *end-- = temp;
            }
        }
    }
    return str;
}
0___________
  • 60,014
  • 4
  • 34
  • 74