i am using strrev function and also included the header file string.h but i am getting the compile error
‘strrev’ was not declared in this scope.

what else is required?

Dani AI

Generated

This error happens because strrev is not part of the C or C++ standard libraries. Some Windows toolchains expose Microsoft’s nonstandard _strrev, so it can appear to work locally (e.g., certain Dev-C++ setups) but fail on stricter compilers and online judges. The standard <string.h> does not include strrev, which is why you see the compile error. See the C header list for confirmation, and Microsoft’s docs for the vendor-specific _strrev. C <string.h> functions. MSVC _strrev.

As and said, write your own. Here’s a small, portable C version that works in place:

#include <string.h>

char *strrev_inplace(char *s) {
    if (!s) return NULL;
    size_t i = 0, j = strlen(s);
    if (j == 0) return s;
    for (--j; i < j; ++i, --j) {
        char t = s[i];
        s[i] = s[j];
        s[j] = t;
    }
    return s;
}

Notes:

  • Pass a modifiable array, not a string literal. For example:
    • OK: char buf[] = "hello"; strrev_inplace(buf);
    • Not OK: strrev_inplace("hello"); // modifies read-only storage
  • If you need a reversed copy, allocate a new buffer and copy characters from the end to the start.

If you’re actually compiling as C++ (your error text suggests g++), prefer the standard algorithm instead of strrev:

#include <algorithm>
#include <string>
std::string s = "hello";
std::reverse(s.begin(), s.end());  // portable

Reference for std::reverse: std::reverse.

This aligns with ’s and ’s points: portability beats extensions, especially on judge systems.

Recommended Answers

All 6 Replies

can i know wat compiler r u using???

some times the compilerdirectory may not have string.h libraray functions.the same problem i had in alloc.h

strrev() isn't a standard function, so compilers aren't obligated to support it. What compiler are you using?

You can always write your own strrev().

in dev-c++ it works fine.
but the problem is when i submit the code on site. it gives me compile error.

Then, as deceptikon says, it's not supported by that compiler. Write your own.

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.