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

📅  最后修改于: 2021-05-26 01:30:26             🧑  作者: Mango

在C / C的wmemchr()函数在++的宽字符块找到字符。此函数在ptr所指向的块的前num个字符内搜索ch的首次出现,并返回指向它的指针。

句法:

参数:该函数接受三个强制性参数,如下所述:

  • ptr:指定要搜索的字符数组的指针。
  • ch:指定要定位的字符。
  • num:指定要比较的wchar_t类型的元素数。

返回值:该函数返回两个值,如下所示:

  • 成功时,它将返回一个指向字符数组位置的指针。
  • 否则,返回NULL指针。

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

计划1:

C++
// C++ program to illustrate
// wmemchr() function
 
#include 
using namespace std;
 
int main()
{
    // initialize the string to be scanned
    wchar_t ptr[] = L"GeeksForGeeks";
 
    // character to be searched for
    wchar_t ch = L'o';
 
    // length, till the character to be search for is 8
    // run the function to check if the character is present
    bool look = wmemchr(ptr, ch, 8);
    if (look)
        wcout << "'" << ch << "'" << L" is present in first "
              << 8 << L" characters of \"" << ptr << "\"";
    else
        wcout << "'" << ch << "'" << L" is not present in first "
              << 8 << L" characters of \"" << ptr << "\"";
 
    return 0;
}


C++
// C++ program to illustrate
// wmemchr() function
 
#include 
using namespace std;
 
int main()
{
    // initialize the string to be scanned
    wchar_t ptr[] = L"GFG";
 
    // character to be searched for
    wchar_t ch = L'p';
 
    // length, till the character to be search for is 3
    // run the function to check if the character is present
    bool look = wmemchr(ptr, ch, 3);
    if (look)
        wcout << "'" << ch << "'" << L" is present in first "
              << 3 << L" characters of \"" << ptr << "\"";
    else
        wcout << "'" << ch << "'" << L" is not present in first "
              << 3 << L" characters of \"" << ptr << "\"";
 
    return 0;
}


输出:
'o' is present in first 8 characters of "GeeksForGeeks"

程序2:

C++

// C++ program to illustrate
// wmemchr() function
 
#include 
using namespace std;
 
int main()
{
    // initialize the string to be scanned
    wchar_t ptr[] = L"GFG";
 
    // character to be searched for
    wchar_t ch = L'p';
 
    // length, till the character to be search for is 3
    // run the function to check if the character is present
    bool look = wmemchr(ptr, ch, 3);
    if (look)
        wcout << "'" << ch << "'" << L" is present in first "
              << 3 << L" characters of \"" << ptr << "\"";
    else
        wcout << "'" << ch << "'" << L" is not present in first "
              << 3 << L" characters of \"" << ptr << "\"";
 
    return 0;
}
输出:
'p' is not present in first 3 characters of "GFG"
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。