Compares the characters in a wide-character string against a
set of wide characters. The function returns the length of the
initial substring that is comprised entirely of characters that
are not in the set of wide characters.
Format
#include <wchar.h>
size_t wcscspn (const wchar_t *wstr1, const wchar_t *wstr2);
1 – Arguments
wstr1
A pointer to a null-terminated wide-character string. If this is
a null string, 0 is returned.
wstr2
A pointer to a null-terminated wide-character string that
contains the set of wide characters for which the function will
search.
2 – Description
The wcscspn function scans the wide characters in the string
pointed to by wstr1 until it encounters a character found in
wstr2. The function returns the length of the initial segment of
wstr1 that is formed by characters not found in wstr2.
3 – Return Value
x The length of the segment.
4 – Example
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
/* This test sets up 2 strings, buffer and w_string, and */
/* then uses wcscspn() to calculate the maximum segment */
/* of w_string, which consists entirely of characters */
/* NOT from buffer. */
#define BUFF_SIZE 20
#define STRING_SIZE 50
main()
{
wchar_t buffer[BUFF_SIZE];
wchar_t w_string[STRING_SIZE];
size_t result;
/* Initialize the buffer */
if (mbstowcs(buffer, "abcdefg", BUFF_SIZE) == (size_t)-1) {
perror("mbstowcs");
exit(EXIT_FAILURE);
}
/* Initialize the string */
if (mbstowcs(w_string, "jklmabcjklabcdehjklmno", STRING_SIZE)
perror("mbstowcs");
exit(EXIT_FAILURE);
}
/* Using wcscspn - work out the largest string in w_string */
/* which consists entirely of characters NOT from buffer */
result = wcscspn(w_string, buffer);
printf("Longest segment NOT found in w_string is: %d", result);
}
Running the example program produces the following result:
Longest segment NOT found in w_string is: 4