C++ cstring strxfrm() Function



The strxfrm() function is a built-in function in C++ which is defined in <cstring> header file. It is used for transforming string using locale-specific collation order.

This function is similar to the strcoll() function but the difference is that strcoll() function compares two strings based on the locale-specific collation order whereas, strxfrm() function transforms the string using locale-specific collation order.

Lets assume we have a project that shows the regional data. In this project, we need to transform the string based on the locale-specific collation order. In such cases, we can use the strxfrm() function.

Syntax

Following is the syntax of the strxfrm() function −

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

Parameters

Parameters of the strxfrm() function are as follows −

  • dest: This is the destination string where the transformed string will be stored.
  • src: This is the source string which will be transformed.
  • n: This is the maximum number of characters to be transformed.

Return Value

The strxfrm() function returns the number of characters that are transformed.

Example

We will take two strings str1 and str2 and then transform them using the strxfrm() function. This example help you to understand the usage of the strxfrm() function.

#include <iostream>
#include <cstring>

using namespace std;

int main() {
   char str1[15];
   char str2[15];
   size_t ret;

   strcpy(str1, "abcd2z");
   strcpy(str2, "ABCDEF");

   ret = strxfrm(str1, str2, 5);

   cout << "Transformed string: " << str1 << endl;
   cout << "Number of characters need to transform the string completely: " << ret << endl;

   return 0;
}

Output

Following is the output of the above C++ program −

Transformed string: ABCDEz
Number of characters need to transform the string completely: 6
Advertisements