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

📅  最后修改于: 2021-05-30 15:37:52             🧑  作者: Mango

C / C++中的wcscspn()函数在给定的宽字符串_1中搜索字符_2的宽字符的首次出现。它是宽字符第一次出现之前返回的宽字符数。搜索包括终止的null宽字符。因此,如果在string_1中找不到string_2的任何字符,则该函数将返回string_1的长度。

句法:

size_t wcscspn( const wchar_t* string_1, const wchar_t* string_2 )

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

  • string_1:指定要搜索的字符串
  • string_2:指定包含要搜索的字符的字符串

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

  • 它返回的宽字符数在STRING_2的任何宽字符的第一次出现在呈现之前STRING_1。
  • 如果在string_1中找不到string_2中的宽字符,则返回string_1的长度

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

// C++ program to illustrate
// wcscspn() function
  
#include 
using namespace std;
  
int main()
{
    // string to be scanned
    wchar_t string_1[] = L"geeksforgeeks012345";
  
    // string containing the character to match
    wchar_t string_2[] = L"0123456789";
  
    // scanning the strings
    int last = wcscspn(string_1, string_2);
  
    // print the position of a matched character
    if (last > wcslen(string_1))
        wcout << string_1 << L" Didn't match any character";
    else
        wcout << L"Occurrence of a character in -> \n"
              << string_1 << " is at position : " << last;
    return 0;
}
输出:
Occurrence of a character in ->
 geeksforgeeks012345 is at position : 13

程序2:

// C++ program to illustrate
// wcscspn() function
#include 
using namespace std;
  
int main()
{
    // string to be scanned
    wchar_t string_1[] = L"GFG";
  
    // string containing the character to match
    wchar_t string_2[] = L"909090909";
  
    // scanning the strings
    int last = wcscspn(string_1, string_2);
  
    // print the position of a matched character
    if (last > wcslen(string_1))
        wcout << string_1 << L" does not contain numbers";
    else
        wcout << L"Length of the string -> "
              << string_1 << " is : " << last;
    return 0;
}
输出:
Length of the string -> GFG is : 3
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”