In this article, we will discuss the strxfrm function in C/C++ with its syntax and examples.
What is strxfrm function?
The strxfrm function is a function in the C/C++ Library. It is used to insert the characters from the source string into the destination string after transforming them into the current locale. It is defined in the C header file <locale.h> . The function strxfrm transforms data so that the output of strcmp on two strings is equal to the output of strcoll on the same pair of original strings.
For instance, let us take two strings: str1 and str2 . In a similar vein, num1 and num2 are two strings created by applying the strxfrm function to convert str1 and str2. In this case, calling the strcmp(num1,num2) is comparable to calling strcoll(str1,str2).
Syntax:
It has the following syntax:
size_t strxfrm(char *str1, const char *str2, size_t num);
Defining parameters:
str1:
It is the string that is given the modified string's num characters.
str2:
The string is what has to be changed.
It is the most characters that can be copied into str1.
Value Returned:
Aside from the final null character, "\0" , the amount of changed characters is returned.
Example:
Let us take an example to illustrate the strxfrm function in C.
Input:
Javacpptutorial
Program:
// C program to demonstrate strxfrm()
#include <stdio.h>
#include <string.h>
// Driver Code
int main()
{
char src[12], dest[12];
int len;
strcpy(src, "javacpptutorial");
len = strxfrm(dest, src, 12);
printf("Transformed string: %s\n", dest);
printf("Length of transformed string: %d\n", len);
return 0;
}
Output:
Transformed string: javacpptutorial
Length of transformed string: 11
Example 2:
Let us take another example to illustrate the strxfrm function in C.
Input:
hello javacpptutorial
Program:
// C program to demonstrate strxfrm()
#include <stdio.h>
#include <string.h>
// Driver Code
int main()
{
char src[20], dest[200];
int len;
strcpy(src, " hello javacpptutorial");
len = strxfrm(dest, src, 20);
printf("Transformed string: %s\n", dest);
printf("Length of transformed string (including spaces): %d\n", len);
return 0;
}
Output:
Transformed string: hello javacpptutorial
Length of transformed string (including spaces): 21
Example 3:
Let us take another example to illustrate the strxfrm function in C++.
// C++ program to demonstrate strxfrm()
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str2[30] = "Hello javacpptutorial";
char str1[30];
cout << "Original string: " << str2 << endl;
// Using strxfrm to transform the string
size_t len = strxfrm(str1, str2, 30);
cout << "Length of transformed string: " << len << endl;
cout << "Transformed string: " << str1 << endl;
cout << "Original string after transformation: " << str2 << endl;
return 0;
}
Output:
Original string: Hello javacpptutorial
Length of transformed string: 17
Transformed string: Hello javacpptutorial
Original string after transformation: Hello javacpptutorial