Scans for the last occurrence of a wide character in a given
string.
Format
#include <wchar.h>
wchar_t *wcsrchr (const wchar_t *wstr, wchar_t wc);
1 – Function Variants
The wcsrchr function has variants named _wcsrchr32 and _wcsrchr64
for use with 32-bit and 64-bit pointer sizes, respectively.
2 – Arguments
wstr
A pointer to a null-terminated wide-character string.
wc
A character of type wchar_t.
3 – Description
The wcsrchr function returns the address of the last occurrence
of a given wide character in a null-terminated wide-character
string. The terminating null character is considered to be part
of the string.
See also wcschr.
4 – Return Values
x The address of the last occurrence of the
specified wide character.
NULL Indicates that the wide character does not
occur in the string.
5 – Example
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#define BUFF_SIZE 50
#define STRING_SIZE 6
main()
{
int i;
wchar_t s1buf[BUFF_SIZE],
w_string[STRING_SIZE];
wchar_t *status;
wchar_t *pbuf = s1buf;
/* Initialize the buffer */
if (mbstowcs(s1buf, "hijklabcdefg ytuhijklfedcba", BUFF_SIZE)
perror("mbstowcs");
exit(EXIT_FAILURE);
}
/* Initialize the string to be searched for */
if (mbstowcs(w_string, "hijkl", STRING_SIZE) == (size_t)-1) {
perror("mbstowcs");
exit(EXIT_FAILURE);
}
/* This program checks the wcsrchr function by searching for */
/* the last occurrence of a string in the buffer s1buf and */
/* prints out the contents of s1buff from the location of
/* the string found. */
status = wcsrchr(s1buf, w_string[0]);
/* Check for pointer to start of rightmost character string. */
if (status == pbuf) {
printf("Error in wcsrchr\n");
exit(EXIT_FAILURE);
}
printf("Program completed successfully\n");
printf("String found : [%S]\n", status);
}
Running the example produces the following result:
Program completed successfully
String found : [hijklfedcba]