Hi,
I am using ubuntu linux system.In that I am unable to use strrev() function for reversing a string.Is there any library function in linux equivalent to strrev().
Thanks in advance.

Dani AI

Generated

Linux/glibc does not provide strrev() because it is not part of ISO C or POSIX. You will not find it in the official POSIX list of string.h functions, so portable code should implement reversing itself or use a library that offers it. On Windows, MSVC ships a nonstandard _strrev (strrev is kept as a deprecated alias), which is why some tutorials mention it. If you need a drop‑in function on Linux, write your own; if you prefer a library call, GLib offers g_strreverse for byte strings and g_utf8_strreverse for UTF‑8. (pubs.opengroup.org, learn.microsoft.com, docs.gtk.org)

Building on what Adak and Narue suggested, here is a small, allocation‑based helper that leaves the original string untouched (handy if your input is a literal or const). It is ASCII/byte‑oriented; for UTF‑8, prefer GLib’s UTF‑8 API noted above.

#include <stdlib.h>
#include <string.h>

char *str_reverse_dup(const char *s) {
    if (!s) return NULL;
    size_t n = strlen(s);
    char *out = malloc(n + 1);
    if (!out) return NULL;
    for (size_t i = 0; i < n; ++i) out[i] = s[n - 1 - i];
    out[n] = '\0';
    return out;  // caller must free(out)
}

Since challarao asked about man pages: section 3 covers C library calls. Try man 3 strlen or search with man -k reverse. If nothing shows up, install developer man pages on Ubuntu and try again:

sudo apt update
sudo apt install manpages-dev

Then explore: man 3 string, man 7 man-pages, and info libc for the GNU C Library manual. See the man(1) description of sections and Ubuntu’s manpages-dev package for details. (man7.org, packages.ubuntu.com)

Note: If you truly need a library function and can add a dependency, GLib’s g_strreverse and g_utf8_strreverse are well‑documented alternatives. (docs.gtk.org)

Recommended Answers

All 8 Replies

A very simple function. Here's a sample program:

#include <stdio.h>
#include <string.h>

int main() {
  int i, j, temp; 
  char s[]="Once a jolly swagman";
  i=0;
  j=strlen(s) - 1;  

  while(i < j) {
    temp = s[i];
    s[i++] = s[j];
    s[j--] = temp;
  }
  printf("\n\n New string is: %s", s);

  printf("\n\n\t\t\t     press enter when ready");
  i = getchar();
  return 0;
}

Check your man pages, strrev might actually be present. If not, it's a trivial function to implement for your personal library:

#include <string.h>

#define SWAP(T, a, b) \
    do { T save = (a); (a) = (b); (b) = save; } while (0)

char *reverse_string(char *s)
{
    size_t len = strlen(s);

    if (len > 1) {
        char *a = s;
        char *b = s + len - 1;

        for (; a < b; ++a, --b)
            SWAP(char, *a, *b);
    }

    return s;
}

Check your man pages, strrev might actually be present. If not, it's a trivial function to implement for your personal library:

#include <string.h>

#define SWAP(T, a, b) \
    do { T save = (a); (a) = (b); (b) = save; } while (0)

char *reverse_string(char *s)
{
    size_t len = strlen(s);

    if (len > 1) {
        char *a = s;
        char *b = s + len - 1;

        for (; a < b; ++a, --b)
            SWAP(char, *a, *b);
    }

    return s;
}

Thanks..
But what are man pages ....Do you mean header files I checked string.h file for strrev().There is no such function in that file.If man page is not header file what is that..?
Is there any predefined strrev() equivalent in GCC..C?
What do you mean by implement...Do I need to add this function to library files..?If so ,to which files..?
Thanks

Thanks..
But what are man pages ....Do you mean header files I checked string.h file for strrev().There is no such function in that file.If man page is not header file what is that..?
Is there any predefined strrev() equivalent in GCC..C?
What do you mean by implement...Do I need to add this function to library files..?If so ,to which files..?
Thanks

man pages or manual pages are like help pages for a particular command or system call or a clibrary function.

check this link http://en.wikipedia.org/wiki/Man_page for more info.

Even, you can just type man 'cmd' in google to get online man pages


for eg: man 3 strrev for info regarding the strrev c library function.

>But what are man pages ....
You program on Linux and don't know how to consult the man pages? Open up a command prompt and type "man". :)

>What do you mean by implement...
I mean do it yourself. If the function doesn't exist, write it.

>But what are man pages ....
You program on Linux and don't know how to consult the man pages? Open up a command prompt and type "man". :)

>What do you mean by implement...
I mean do it yourself. If the function doesn't exist, write it.

I am newbie and our tutorials didn't mention about man pages.I opened now man pages but don't know what to do with it.
Thanks

How can I delete a post.?

I am newbie and our tutorials didn't mention about man pages.I opened now man pages but don't know what to do with it.
Thanks

Why dont you take effort in reading the link i posted above. It shows usage
only thing u need to do with man pages now is to read, understand and apply it in your application
if you need some more information,
'info' command will also help> Sometimes it has more details than man pages
and if you dont know how to use info command, try 'man info'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.