Strxfrm Function In Cc++ - C++ Programming Tutorial
C++ Course / Functions / Strxfrm Function In Cc++

Strxfrm Function In Cc++

BLUF: Mastering Strxfrm Function In Cc++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Strxfrm Function In Cc++

C++ is renowned for its efficiency. Learn how Strxfrm Function In Cc++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the strxfrm function in C/C++ along with its syntax and sample illustrations.

What is strxfrm function?

The strxfrm function is a part of the C/C++ Library and serves the purpose of copying characters from the source string to the destination string, following a transformation based on the current locale settings. This function is declared in the C header file <locale.h>. By utilizing strxfrm, the data is modified to ensure that when comparing two strings using strcmp, the result matches the output of strcoll for the original strings.

For example, consider two variables: string1 and string2. Similarly, number1 and number2 represent two strings generated by utilizing the strxfrm function to transform string1 and string2. In this scenario, invoking the strcmp(number1, number2) is analogous to invoking strcoll(string1, string2).

Syntax:

It has the following syntax:

Example

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

Defining parameters:

str1:

It is the string that determines the length of the altered string to num characters.

str2:

The string is what has to be changed.

It represents the maximum number of characters that can be copied into the variable str1.

Value Returned:

Apart from the terminating null character, "\0", the function returns the number of modified characters.

Example:

Let's consider a scenario to demonstrate the application of the strxfrm function in the C programming language.

Input:

Javacpptutorial

Program:

Example

// 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:

Output

Transformed string: javacpptutorial
Length of transformed string: 11

Example 2:

Let's consider another instance to demonstrate the strxfrm function in the C programming language.

Input:

hello javacpptutorial

Program:

Example

// 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:

Output

Transformed string: hello javacpptutorial
Length of transformed string (including spaces): 21

Example 3:

Let's consider a different instance to demonstrate the strxfrm function in C++.

Example

// 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:

Output

Original string: Hello javacpptutorial
Length of transformed string: 17
Transformed string: Hello javacpptutorial
Original string after transformation: Hello javacpptutorial

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience