Open In App

wcschr() function in C++

Last Updated : 14 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

wcschr() function in C++ searches for the first occurrence of a wide character in a wide string. The terminating null wide character is considered part of the string. Therefore, it can also be located in order to retrieve a pointer to the end of a wide string.

Syntax:

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

Parameters: The function accepts two mandatory parameters which are described below:

  1. ws: Pointer to the null terminated wide string to be searched for
  2. wc: Wide character to be located

Return values: The function returns two value as below:

  1. A pointer to the first occurrence of a character in a string.
  2. If character is not found, the function returns a null pointer.

Below programs illustrate the above function:
Program 1 :




// C++ program to illustrate
// wcschr() function
#include <bits/stdc++.h>
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;
}


Output:

Looking for the 'o' character in "This is some good coding"...
found at 10
found at 15
found at 16
found at 20

Program 2 :




// C++ program to illustrate
// wcschr() function
#include <bits/stdc++.h>
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;
}


Output:

Looking for the 'g' character in "geekforgeeks"...
found at 1
found at 8


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads