Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the strrev() function not available in Linux?

Tags:

c

string

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

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

Doesn't Linux support strrev()?

like image 468
Dinesh Avatar asked Dec 16 '11 06:12

Dinesh


People also ask

Why is Strrev not working?

strrev is non standard and deprecated function. So dont work on modern compilers..

Is Strrev a built in function?

The strrev() function is a built-in function in C and is defined in string. h header file. The strrev() function is used to reverse the given string.

Why Strrev is not working in C?

Unfortunately, you cannot use strrev function here. In this case, you have to write the complete function that works the same as an inbuilt function. You might be facing the same issue while using strstr function. You can write your own code to implement strstr() function.


2 Answers

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; } 
like image 140
Ignacio Vazquez-Abrams Avatar answered Sep 18 '22 19:09

Ignacio Vazquez-Abrams


#include <string.h>  char *strrev(char *str) {     if (!str || ! *str)         return 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; } 
like image 26
Sumit Naik Avatar answered Sep 20 '22 19:09

Sumit Naik