![]() |
VOOZH | about |
In C++, characters are represented using the char data type, which is designed to hold a single byte. However, there are many languages other than English, and all these characters cannot be represented by a single byte. These characters are known as multi-byte characters. In this article, we will learn how to handle multi-byte characters in C++.
Example:
Input:
A multi-byte character
Output:
// Hello in Japanese
こんにちは.
C++ provides the wchar_t data type to handle the multi-byte characters. It is similar to the char data type but it allows stores the wide characters instead. These wide characters are 2 bytes in size. We can create a sequence of these wide characters to make a string solely of wide characters. The std::wstring is such a string object defined inside the <string> header file.
Before that, we need to enable all the language formatting using the std::setlocate() function.
The below program demonstrates how we can handle multi-byte characters in C++.
Output
こんにちはTime Complexity: O(N) where N is the number of characters in the string.
Auxiliary Space: O(N)