Skip to content
Related Articles

Related Articles

strxfrm() in C/C++

Improve Article
Save Article
  • Last Updated : 04 Jan, 2022
Improve Article
Save Article

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




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

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

This article is contributed by Shivani Baghel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!