strxfrm() in C/C++

The c/c++ library strxfrm() transform the characters of source string into current locale and place them in destination string.
For that LC_COLLATE category is used which is defined in locale.h . strxfrm() function performs transformation in such a way that result of strcmp on two strings is the same as result of strcoll on two original strings.
Syntax:

size_t strxfrm(char *str1, const char *str2, size_t num);
parameters:
str1 :is the string which receives 
num characters of transformed string. 
str2 : is the string which is to be transformed
num  :is the maximum number of characters
which to be copied into str1.

Examples :

Input : 'geeksforgeeks'
Output : 13
filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate
// strxfrm()
#include <stdio.h>
#include <string.h>
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);
}

chevron_right


OUTPUT:

Length of string geeksforgeeks is: 13

Example 2 :

Input : 'hello geeksforgeeks'
Output : 20 // in this example it count space also 
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


OUTPUT:

Length of string  hello geeksforgeeks is: 20

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


OUTPUT:

19
Hell
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 contribute.geeksforgeeks.org or mail your article to contribute@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


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.