How Do I Handle Multi-Byte Characters in C++?
Last Updated :
08 Apr, 2024
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
こんにちは.
Create a Multi-Byte Characters in C++
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.
Approach
- Set the locale to handle multi-byte characters using the std::setlocale function.
- Create a wide-character string using std::wstring which can store the multi-byte characters.
- Print the wide-character string using std::wcout.
C++ Program to Handle Multi-Byte Characters
The below program demonstrates how we can handle multi-byte characters in C++.
C++
// C++ program to demonstrates how we can handle multi-byte
// characters
#include <iostream>
#include <locale>
using namespace std;
int main()
{
// Setting the locale
setlocale(LC_ALL, "");
// Creating a wide character string
wstring str = L"こんにちは";
// Printing the wide character string
wcout << str << endl;
return 0;
}
Output
こんにちは
Time Complexity: O(N) where N is the number of characters in the string.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...