C++ strxfrm()

The strxfrm() function in C++ transforms a given null terminated byte string into an implementation defined form.

THe strxfrm() function transforms the string such that comparing two transformed string using strcmp() function produces identical result as comparing the original strings using strcoll() function in the current C locale.

For example, x and y are two strings. a and b are two strings formed by transforming x and y respectively using the strxfrm function. Then a call to strcmp(a,b) is same as calling strcoll(x,y).

strxfrm() prototype

size_t strxfrm(char* dest, const char* src, size_t count);

The strxfrm() function converts the first count characters of the string pointed to by src to an implementation defined form and the result is stored in the memory location pointed to by dest.

The behavior of this function is undefined if:

  • size of dest is less than the required size.
  • dest and src overlap.

It is defined in <cstring> header file.

strxfrm() Parameters

  • dest: pointer to the array where the transformed string is stored.
  • src: pointer to the null terminated string to be transformed.
  • count: maximum number of characters to convert.

strxfrm() Return value

The strxfrm() function returns the number of character transformed, excluding the terminating null character '\0'.

Example: How strxfrm() function works?

#include <iostream>
#include <cstring>
#include <clocale>
using namespace std;
int main()
	setlocale(LC_COLLATE, "cs_CZ.UTF-8");
	const char* s1 = "hrnec";
	const char* s2 = "chrt";
	char t1[20], t2[20];

	cout << "strcoll returned " << strcoll(s1,s2) << endl;
	cout << "Before transformation, " << "strcmp returned " << strcmp(s1,s2) << endl;

	cout << "After transformation, " << "strcmp returned " << strcmp(t1,t2) << endl;

	return 0;

When you run the program, the output will be:

strcoll returned -1
Before transformation, strcmp returned 1
After transformation, strcmp returned -1
Did you find this article helpful?