📜  C++中的wcschr()函数

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

C++中的wcschr()函数在宽字符串搜索宽字符的首次出现。终止的null宽字符被视为字符串的一部分。因此,还可以定位它以便检索指向宽字符串末尾的指针。

句法:

const wchar_t* wcschr (const wchar_t* ws, wchar_t wc)
      wchar_t* wcschr (      wchar_t* ws, wchar_t wc)

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

  1. ws:指向要搜索的以空终止的宽字符串的指针
  2. wc:要定位的宽字符

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

  1. 指向字符串字符首次出现的指针。
  2. 如果找不到字符,则该函数返回空指针。

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

// C++ program to illustrate
// wcschr() function
#include 
using namespace std;
  
int main()
{
    // initialize wide string
    wchar_t ws[] = L"This is some good coding";
  
    wchar_t* point;
    wprintf(L"Looking for the 'o' character in \"%ls\"...\n", ws);
  
    // initialize the search character
    point = wcschr(ws, L'o');
  
    // search the place and print
    while (point != NULL) {
        wprintf(L"found at %d\n", point - ws + 1);
        point = wcschr(point + 1, L'o');
    }
  
    return 0;
}
输出:
Looking for the 'o' character in "This is some good coding"...
found at 10
found at 15
found at 16
found at 20

程序2:

// C++ program to illustrate
// wcschr() function
#include 
using namespace std;
  
int main()
{
    // initialize wide string
    wchar_t ws[] = L"geekforgeeks";
  
    wchar_t* point;
    wprintf(L"Looking for the 'g' character in \"%ls\"...\n", ws);
  
    // initialize the search character
    point = wcschr(ws, L'g');
  
    // search the place and print
    while (point != NULL) {
        wprintf(L"found at %d\n", point - ws + 1);
        point = wcschr(point + 1, L'g');
    }
  
    return 0;
}
输出:
Looking for the 'g' character in "geekforgeeks"...
found at 1
found at 8
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”