mbrlen(): This function is available in the file wchar.h. This function returns the size of the multibyte character pointed by pmb, examining at most max bytes. This function returns:
1.If pmb points to a null character, or if pmb is a null pointer, the function returns zero.
2.Otherwise, if at most max characters pointed by pmb form a valid multibyte character, the function returns the size in bytes of that multibyte character.
Parameters: Three parameters are required for this function.
pmb: Pointer to the first byte of a multibyte character.
max: Maximum number of bytes to check. The macro constant MB_CUR_MAX defines the maximum number of bytes that can form a multibyte character under the current locale settings.
ps: Pointer to a mbstate_t object that defines a conversion state.
Syntax:
size_t mbrlen(const char * pmb, size_t max, mbstate_t *ps)
Approach
C++
#include <bits/stdc++.h>using namespace std;string fun(char *ch, size_t len){mbstate_t mbs;mbrlen(NULL, 0, &mbs);size_t length;string str = "";while (len > 0){length = mbrlen(ch, len, &mbs);//if the length is zero or length is greater than//remainig len of the string break out of loopif (length == 0 || length > len)break;// appends all the characters into resfor (int i = 0; i < length; i++)str += (*ch++);//decrease the orginal len by lengthlen -= length;}return str;}int main(){char ch[] = "Hello World!";cout << fun(ch, sizeof(ch)) << "\n";return 0;}
Output: Hello World!
No comments:
Post a Comment