AppDividend
Latest Code Tutorials

C++ strxfrm() Function Example | strxfrm() in C++

0

C++ strxfrm() library converts a given string into an implementation-defined form. It converts the string into a current locale, and then it is placed in the defined form. The transformation is done in such a way that after transformation of two strings, the strings shown by strcmp() and strcoll() on the given two strings are the same.

C++ strxfrm()

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

For Example, suppose we take two strings str1 and str2, and after transforming string using strxfrm(), it becomes strA and strB. Then if we call strcmp() by passing strA and strB as parameters, respectively, we will get the same results as by calling strcoll() by passing str1 and str2 as parameters.

Syntax

size_t strxfrm(char *a, const char *b, size_t num);

In the above syntax, the function converts the total ‘num’ characters of the given string pointed to by ‘b’ to a defined form, and finally, it is stored in the location, which is pointed by a.

Parameters

a: It is a pointer to the string which is going to be transformed.

b: It is the pointer to the array where the string after getting transformed will be stored.

count: It is the numbers of characters from the original string, which is about to be converted.

Return Value

It returns the total number of characters that are being transformed expect the null character.

For Example, if we input “hello” using this function, then the output will be 5.

Example programs on C++ strxfrm()

Example 1: Write a program to show the mechanism of the strxfrm() function.

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
  char a[10] = "hello", b[10];
  int k;
  k = strxfrm(b, a, 10);
  cout << "Length of the transformed string=" << k << endl;
  cout << "Transformed string = " << b;
  return 0;
}

Output

Length of the transformed string=5
Transformed string = hello

Example 2: Write a program to use strxfrm() on only some characters of the main string and print the output.

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
  char a[10] = "hello", b[10];
  int k;
  k = strxfrm(b, a, 10);
  cout << "Length of the transformed string=" << k << endl;
  cout << "Transformed string = " << b;
  return 0;
}

Output

Length of the converted string = 11
Original String=Hello World
Converted string=Hello

See also

C++ strcoll()

C++ memset()

C++ strerror()

C++ strlen()

C++ strstr()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.