strxfrm() in C/C++

strxfrm() is a C/C++ Library function. It is used to transform the characters of the source string into the current locale and place them in the destination string. It is defined in the <locale.h> header file in C. strxfrm() function performs transformation in such a way that the result of strcmp on two strings is the same as a result of strcoll on two original strings. 

For example, str1 and str2 are two strings. Similarly, num1 and num2 are two strings formed by transforming str1 and str2 respectively using the strxfrm function. Here, calling strcmp(num1,num2) is similar as calling as strcoll(str1,str2).

Syntax: 

size_t strxfrm(char *str1, const char *str2, size_t num);

Parameters:

  • str1: It is the string that receives num characters of the transformed string.
  • str2: It is the string that is to be transformed.
  • num: It is the maximum number of characters to be copied into str1.

Return Value: It returns the number of characters transformed( excluding the terminating null character ‘\0’).

Example 1:

Input

'w3wiki'

C





Output

Length of string Beginnerforge@ is: 13

Example 2: 

Input

'hello w3wiki' 

Note: In this example, the spaces will be counted too.

C




// C program to demonstrate strxfrm()
#include <stdio.h>
#include <string.h>
int main()
{
    char src[20], dest[200];
    int len;
    strcpy(src, " hello w3wiki");
    len = strxfrm(dest, src, 20);
    printf("Length of string %s is: %d", dest, len);
 
    return (0);
}


Output

Length of string  hello w3wiki9 is: 20

Example in C++:

CPP




// C program to demonstrate strxfrm()
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char str2[30] = "Hello w3wiki";
    char str1[30];
    cout << strxfrm(str1, str2, 4) << endl;
    cout << str1 << endl;
    cout << str2 << endl;
    return 0;
}


Output

19
HellL
Hello w3wiki


Contact Us