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()
?
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()
?
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;
}
strrev
implementations... –
Macfadyn *p1
does not modify *p2
in your implementation, allowing to move assignments across read accesses for the purpose of simplifying them, you should write compilers instead of wasting your time on StackOverflow. clang-5.1 -O3 generates three reads and three writes, perhaps you should give them a hand. –
Nothing p2 != p1
in a for
loop with a condition that is p2 > p1
? Wow. –
Flynn #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;
}
str
is empty, strlen(str) - 1
is garbage. If strlen(str)
is bigger than INT_MAX
, you're also going to have problems. –
Sessoms NULL
pointer and empty string check. strlen()
returns size_t
and handling INT_MAX
should be done the library implementation. –
Aspidistra To accurately answer your question,
Is strrev() not available on Linux?
The functions strrev() available in the string.h library. Functions strrev() including some other string functions such as like strupr(), strlwr(), strrev(), which are only available in ANSI C (Turbo C/C++) and are not available in the standard C-GCC compiler.
It’s not about the system. It is about the C compiler you are using.
References:
https://discuss.codechef.com/t/is-strrev-function-not-available-in-standard-gcc-compiler/2449
Unfortunately, strrev
seems to be absent from glibc's string.h
.
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;
}
strrev()
performed in place reversal. This code does not. –
Evangel readln
I use, can be in place. –
Colorcast How about this:
#include <string.h>
char *strrev(char *s)
{
if (s && *s) {
char *b = s, *e = s + strlen(s) - 1;
while (b < e) {
char t = *b;
*b++ = *e;
*e-- = t;
}
}
return s;
}
There is no string library function to reverse a string.
strrev()
Is not present in GCC compiler in Linux.
Make your own reverse function:
reverse.c
:
/*
* C program to reverse a 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;
}
reverse(str1, index + 1, size);
}
At the time I needed to terminate the source string at a given point, then reverse:
// terminate and reverse string
char* tstrrev(char* s, int pt)
{
char *r = malloc( (pt+1) * sizeof(char) );
s[pt] = r[pt] = '\0';
s+= pt - 1;
while ( *s )
*r++ = *s--;
return r-pt;
}
© 2022 - 2024 — McMap. All rights reserved.
strrev()
is not part of any C standard but it is commonly included in some library implementations. – Evangelstr
and a lowercase letter are reserved to the implementation.str_rev
is OK. – Galer