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


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

在頭文件cwctype.h中指定了C /C++中的wctrans(),並返回了與轉換相對應的wctrans_t類型的值。特定的語言環境可以接受字符的多種轉換。以下是一些公認的轉換:

  • “tolower”->小寫| wl
  • “toupper”->大寫|拖曳

用法:

wctrans_t wctrans( const char* string )

參數:該函數接受一個強製性參數字符串,該字符串指定標識字符轉換的字符串。


返回值:該函數返回兩個值,如下所示:

  • 如果當前語言環境未提供映射,則它將返回零。
  • 否則,它返回可以與towctrans()一起用於映射寬字符的對象。

以下示例程序旨在說明上述函數:
程序1:

// C++ program to illustrate 
// towctrans() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initilize the string 
    wchar_t string[] = L"GeeksForGeeks"; 
    wcout << L"Initial string -> " << string << endl; 
  
    wctype_t first = wctype("lower"); 
    wctype_t second = wctype("upper"); 
  
    // traverse each character and convert 
    // lower case to upper and upper to lower 
    for (int i = 0; i < wcslen(string); i++) { 
        if (iswctype(string[i], first)) 
            string[i] = towctrans(string[i], wctrans("toupper")); 
        else if (iswctype(string[i], second)) 
            string[i] = towctrans(string[i], wctrans("tolower")); 
    } 
  
    // print the string after transformation 
    wcout << L"After transformation -> " << string << endl; 
  
    return 0; 
}
輸出:
Initial string -> GeeksForGeeks
After transformation -> gEEKSfORgEEKS

程序2:

// C++ program to illustrate 
// towctrans() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initilize the string 
    wchar_t string[] = L"gfg"; 
    wcout << L"Initial string -> " << string << endl; 
  
    wctype_t first = wctype("lower"); 
    wctype_t second = wctype("upper"); 
  
    // traverse each character and convert 
    // lower case to upper and upper to lower 
    for (int i = 0; i < wcslen(string); i++) { 
        if (iswctype(string[i], first)) 
            string[i] = towctrans(string[i], wctrans("toupper")); 
        else if (iswctype(string[i], second)) 
            string[i] = towctrans(string[i], wctrans("tolower")); 
    } 
  
    // print the string after transformation 
    wcout << L"After transformation -> " << string << endl; 
  
    return 0; 
}
輸出:
Initial string -> gfg
After transformation -> GFG


相關用法


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