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.

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.