LIBRARY FUNCTIONS FOR STRINGS

The standard library provides the cstring header that contains functions that operate on null-terminated strings. These are a set of functions that are specified in the C++ standard. There are also alternatives to some of these functions that are not standard, but which provide a more secure implementation of the function than the original versions. In general, I’ll mention both where they exist in the cstring header, but I’ll use the more secure versions in examples. Let’s explore some of the most useful functions provided by the cstring header.

NOTE The string standard header for native C++ defines the string and wstring classes that represent character strings. The string class represents strings of characters of type char and the wstring class represents strings of characters of type wchar_t. Both are defined in the string header as template classes that are instances of the basic_string<T> class template. A class template is a parameterized class (with parameter T in this case) that you can use to create new classes to handle different types of data. I won’t be discussing templates and the string and wstring classes until Chapter 8, but I am mentioning them here because they are much better and safer to use.

Finding the Length of a Null-Terminated String

The strlen() function returns the length of the argument string of type char* as a value of type size_t. The type size_t is an implementation-defined type that corresponds to an unsigned ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.