wcsrchr() — Locate last wide character in string

Standards

Standards / Extensions C or C++ Dependencies

ISO C Amendment
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <wchar.h>

wchar_t *wcsrchr(const wchar_t *string, wchar_t character);

General description

Locates the last occurrence of character in the string pointed to by string. The terminating NULL wide character is considered to be part of the string.

The behavior of this wide-character function is affected by the LC_CTYPE category of the current locale. If you change the category, undefined results can occur.

Returned value

If successful, wcsrchr() returns a pointer to the character.

If character does not occur in the string, wcsrchr() returns NULL.

Example

CELEBW17
⁄* CELEBW17                                      

   This example compares the use of wcschr() and wcsrchr().                     
   It searches the string for the first and last occurrence of p in the         
   wide character string.                                                       

 *⁄                                                                             
#include <stdio.h>                                                              
#include <wchar.h>                                                              
                                                                                
#define SIZE 40                                                                 
                                                                                
int main(void)                                                                  
{                                                                               
  wchar_t buf[SIZE] = L"computer program";                                      
  wchar_t * ptr;                                                                
  int    ch = 'p';                                                              
                                                                                
  ⁄* This illustrates wcschr *⁄                                                 
  ptr = wcschr( buf, ch );                                                      
  printf( "The first occurrence of %c in '%ls' is '%ls'\n", ch, buf, ptr );     
                                                                                
  ⁄* This illustrates wscrchr *⁄                                                
  ptr = wcsrchr( buf, ch );                                                     
  printf( "The last occurrence of %c in '%ls' is '%ls'\n", ch, buf, ptr );      
}                                                                               
Output:
The first occurrence of p in 'computer program' is 'puter program'
The last occurrence of p in 'computer program' is 'program'

Related information