Home C++ Strings Functions strxfrm() C++ strxfrm() Function Definition In C++, strxfrm() transforms a C-string into another byte sequence that can be compared with strcmp to get locale-aware ordering.
Include <cstring>. Related: strcoll.
This guide shows the C library helper in a C++ .cpp program with std::cout. Prefer std::string for new text code; keep these APIs for C interop and interviews.
Syntax Pass destination, source, and destination capacity.
std::size_t n = std::strxfrm(dest, src, dest_size); Call What it does std::strxfrm(dest, src, n)Write transformed form into dest (at most n bytes). std::strxfrm(nullptr, src, 0)Return length needed (not including the idea of writing).
If n is 0 and dest is null, strxfrm returns the length required for the transformed string.
Library declarations size_t strxfrm(char* dest, const char* src, size_t n); Parameters Parameter Description dest Output buffer (or null when querying length). src Source C-string. n Capacity of dest.
Return Value Return Value Description size_tLength of the transformed string (not counting terminator in the usual description—check your libc docs).
C++ Examples 1. Needed length 2. Transform copy 3. Ordering idea Example 1 — Query Length Ask how long the transform is.
#include <cstring>
#include <iostream>
int main() {
const char* src = "ab";
std::size_t need = std::strxfrm(nullptr, src, 0);
std::cout << need << '\\n';
return 0;
} How It Works Passing null dest with n=0 requests the size. For the C locale, transform of short ASCII often matches length. Exact value can vary by locale. Example 2 — Write Transform Transform into a buffer then strcmp.
#include <cstring>
#include <iostream>
int main() {
char a[8];
char b[8];
std::strxfrm(a, "ab", sizeof a);
std::strxfrm(b, "ab", sizeof b);
std::cout << std::strcmp(a, b) << '\\n';
return 0;
} How It Works Both sources are equal. Transformed forms compare equal with strcmp. Result 0. Example 3 — Same Locale Idea Equal ASCII strings compare equal after transform.
#include <cstring>
#include <iostream>
int main() {
char x[8];
char y[8];
std::strxfrm(x, "a", sizeof x);
std::strxfrm(y, "b", sizeof y);
std::cout << (std::strcmp(x, y) < 0 ? "a<b" : "a>=b") << '\\n';
return 0;
} How It Works Transforms are produced for a and b. strcmp on transforms orders them. In the C locale, a comes before b. Frequently Asked Questions What does strxfrm do? Transforms a string for locale-sensitive strcmp comparisons.
Related function? strcoll compares with locale rules directly.
Do beginners need it? Rarely—only when sorting text under a locale.
How to size the buffer? Call strxfrm(nullptr, src, 0) then allocate.
Back to the function index See every string-related function guide in one place.
Function index → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion