C++ strxfrm() function converts a given string into an implementation-defined form. It converts the string into a current locale, then places it in the defined form.
The transformation is done so that after the transformation of two strings, the strings shown by strcmp() and strcoll() on the given two strings are the same.
The C/C++ library strxfrm() transforms the source string’s characters into the current locale and places them in the destination string.
The LC_COLLATE category is used, defined in the locale.h.
The strxfrm() function performs transformation in such a way that the result of strcmp on two strings is the same as that of strcoll on two original strings.
For Example, suppose we take two strings, str1, and str2, and after transforming the string using strxfrm(), it becomes strA and strB. Then, if we call strcmp() by passing strA and strB as parameters, respectively, we will get the same results as by calling strcoll() by passing str1 and str2 as parameters.
Syntax
size_t strxfrm(char *a, const char *b, size_t num);
In the above syntax, the function converts the total ‘num’ characters of the given string pointed to by ‘b’ to a defined form, and finally, it is stored in the location, which is pointed by a.
Parameters
a: It is a pointer to the string which is going to be transformed.
b: It is the pointer to the array where the string, after getting transformed, will be stored.
count: It is the number of characters from the original string, which is about to be converted.
Return Value
It returns the total number of characters that are being transformed except the null character.
For Example, if we input “hello” using this function, the output will be 5.
Example programs on C++ strxfrm()
Example 1: Write a program to show the mechanism of the strxfrm() function.
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int main() { char a[10] = "hello", b[10]; int k; k = strxfrm(b, a, 10); cout << "Length of the transformed string=" << k << endl; cout << "Transformed string = " << b; return 0; }
Output
Length of the transformed string=5 Transformed string = hello
Example 2: Write a program to use strxfrm() on only some characters of the main string and print the output.
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int main() { char a[10] = "hello", b[10]; int k; k = strxfrm(b, a, 10); cout << "Length of the transformed string=" << k << endl; cout << "Transformed string = " << b; return 0; }
Output
Length of the converted string = 11 Original String=Hello World Converted string=Hello
That’s it for this function.