當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ strxfrm()用法及代碼示例


c /c ++庫strxfrm()將源字符串的字符轉換為當前語言環境,並將其放置在目標字符串中。對於該LC_COLLATE類別,該類別在locale.h中定義。 strxfrm()函數執行轉換的方式是,兩個字符串上的strcmp結果與兩個原始字符串上的strcoll結果相同。句法:

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.

例子:

Input:'geeksforgeeks'
Output:13
// 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); 
}

輸出:


Length of string geeksforgeeks is:13

範例2:

Input:'hello geeksforgeeks'
Output:20 // in this example it count space also 
// 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); 
}

輸出:

Length of string  hello geeksforgeeks is:20

範例3:

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

輸出:

19
Hell
Hello geeksforgeeks


相關用法


注:本文由純淨天空篩選整理自 strxfrm() in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。