Open In App

strxfrm() in C/C++

Improve
Improve
Like Article
Like
Save
Share
Report

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

'geeksforgeeks'

C





Output

Length of string geeksforge@ is: 13

Example 2: 

Input

'hello geeksforgeeks' 

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 geeksforgeeks");
    len = strxfrm(dest, src, 20);
    printf("Length of string %s is: %d", dest, len);
 
    return (0);
}


Output

Length of string  hello geeksforgeeks9 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 geeksforgeeks";
    char str1[30];
    cout << strxfrm(str1, str2, 4) << endl;
    cout << str1 << endl;
    cout << str2 << endl;
    return 0;
}


Output

19
HellL
Hello geeksforgeeks


Last Updated : 04 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments