Skip to content
Related Articles

Related Articles

wcsstr() function in C++ with example
  • Last Updated : 22 Jul, 2019

The wcsstr() function is defined in cwchar.h header file. The wcsstr() function finds the first occurrence of a source in a destination string.

Syntax:

wchar_t* wcsstr(const wchar_t* dest, const wchar_t* src);

Parameter: This method accepts the following parameters:

  • src: It represents the pointer to the null terminating wide string to be search for.
  • dest: It represents the pointer to the null terminating wide string in which we have to search.

Return Value: The return value of this method depeneds on:

  • If src is not found than it returns NULL
  • If src points to an empty string then destination is return
  • If the src is found, the wcsstr() function returns the pointer to the first wide character of the src in dest.

Below program illustrate the above function:



Example 1: When the source is not found, null is returned




// c++ program to demonstrate
// example of wcsstr() function.
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // initialize the destination string
    wchar_t dest[] = L"Geeks Are Geeks";
  
    // get the source string to be found
    wchar_t src[] = L"To";
  
    // Find the occurrence and print it
    wcout << wcsstr(dest, src);
  
    return 0;
}
Output:

Example 2: When the source is empty, destination string is returned




// c++ program to demonstrate
// example of wcsstr() function.
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // initialize the destination string
    wchar_t dest[] = L"Geeks Are Geeks";
  
    // get the source string to be found
    wchar_t src[] = L"";
  
    // Find the occurrence and print it
    wcout << wcsstr(dest, src);
  
    return 0;
}
Output:
Geeks Are Geeks

Example 3: When the source is found, the destination from the source is returned.




// c++ program to demonstrate
// example of wcsstr() function.
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // initialize the destination string
    wchar_t dest[] = L"Geeks Are Geeks";
  
    // get the source string to be found
    wchar_t src[] = L"Are";
  
    // Find the occurrence and print it
    wcout << wcsstr(dest, L"Are");
  
    return 0;
}
Output:
Are Geeks
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :