mbrlen() in C++

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 *chsize_t len)
{
    mbstate_t mbs;

        mbrlen(NULL0, &mbs);
    size_t length;
    string str = "";
    while (len > 0)
    {
        length = mbrlen(chlen, &mbs);

        //if the length is zero or length is greater than
        //remainig len of the string break out of loop
        if (length == 0 || length > len)
            break;

        // appends all the characters into res
        for (int i = 0i < lengthi++)
            str += (*ch++);

        //decrease the orginal len by length
        len -= length;
    }

    return str;
}
int main()
{
    char ch[] = "Hello World!";

    cout << fun(chsizeof(ch)) << "\n";
    return 0;
}


Output: Hello World!

No comments:

Post a Comment