How to Make Room for Wide Strings in C++

By Stephen R. Davis

The C++ programmer is often required to manipulate wide character strings. The standard C++ library includes functions to handle wide character strings. A few of these functions are listed in this table.

Wide String-Handling Functions
Name Operation
int wcslen(string) Returns the number of wide characters in a string, not
including the terminating null.
wchar_t* wcscpy(target, source) Copies the source wide string into a target array.
wchar_t* wcscat(target, source) Concatenates the source wide string onto the end of the target
wide string.
wchar_t* wcsncpy(target, source,
Copies a wide string up to n
characters from the source string into a target array.
wchar_t* wcsncat(target, source,
Concatenates the source string onto the end of the target
string or n characters, whichever comes
wchar_t* wcsstr(string, pattern) Finds the address of the first occurrence of pattern in string.
Returns a null if pattern is not found.
int wcscmp(source1, source2) Compares two wide strings. Returns –1 if source1 occurs before source2 in the dictionary and 1 if later.
Returns 0 if the two strings match exactly.
int wcsncmp(source1, source2,
Compares the first n wide characters
in two wide strings.

Remember that wide characters are used for applications that must support foreign languages, where a measly 255 different characters may not be enough.

The following shows a wide character version of the Concatenate program:

// ConcatenateWide - concatenate two wide strings
//     with a " - " in the middle using library routines
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
    // read first string...
    wchar_t wszString1[260];
    cout << "Enter string #1:";
    wcin.getline(wszString1, 128);
    // the second string...
    wchar_t wszString2[128];
    cout << "Enter string #2:";
    wcin.getline(wszString2, 128);
    // now tack the second onto the end of the first
    // with a dash in between
    wcsncat(wszString1, L" - ", 260);
    wcsncat(wszString1, wszString2, 260);
    wcout << L"n" << wszString1 << endl;
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    return 0;

The wide character string program looks similar to its single-byte character string cousin except for the following differences:

  • Variables are declared wchar_t rather than char.

  • Constant characters and constant strings appear preceded by an L, as in L“This is a wide string”.

  • The objects wcin and wcout are used in place of cin and cout for input and output.

  • The wcs… functions appear in place of the narrow str… functions.

The output from ConcatenateWide appears identical to that of the char-based Concatenate program to those who do most of their input/output in European languages. The topic of writing programs capable of handling multiple languages with different alphabets and rules of grammar is known as localization.

ANSI C++ includes a type string designed to make it easier to manipulate strings of text.