📜  C / C++中的wctrans()函数

📅  最后修改于: 2021-05-30 08:10:05             🧑  作者: Mango

C / C++中的wctrans()在头文件cwctype.h中指定,并返回与转换相对应的wctrans_t类型的值。特定的语言环境可以接受字符的多种转换。以下是一些公认的转换:

  • “ tolower” ->小写| wl
  • “ toupper” ->转换为大写字母|拖曳

句法:

wctrans_t wctrans( const char* string )

参数:该函数接受一个强制性参数字符串,该字符串指定标识字符转换的字符串。
返回值:函数返回两个值,如下所示:

  • 如果当前语言环境不提供映射,则它返回零。
  • 否则,它返回可与towctrans()一起用于映射宽字符。

下面的程序说明了上述函数:
程序1:

CPP
// C++ program to illustrate
// towctrans() function
 
#include 
using namespace std;
 
int main()
{
    // initialize 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;
}


CPP
// C++ program to illustrate
// towctrans() function
 
#include 
using namespace std;
 
int main()
{
    // initialize 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 -> GeeksForGeeks
After transformation -> gEEKSfORgEEKS

程序2:

CPP

// C++ program to illustrate
// towctrans() function
 
#include 
using namespace std;
 
int main()
{
    // initialize 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
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。