Ch10: Char Manipulation / Strings¶
TL;DR: Low-Level C-Strings: C-strings are null-terminated character arrays (
char[]) requiring manual memory management and explicit boundary awareness to avoid overflows.
โก Quick Reference¶
#include <iostream>
#include <string>
#include <string_view>
#include <cctype>
#include <cstring>
int main() {
// std::string - Standard dynamic string class supporting manipulation.
std::string str = "Hello C++";
// std::string_view - Lightweight read-only non-owning window (C++17).
std::string_view view = str;
// std::isalnum, std::isalpha, std::isdigit, std::isspace - Char property checking.
bool is_letter = std::isalpha('A');
// std::toupper, std::tolower - Character casing transformations.
char upper = std::toupper('a');
char buffer[20] = "Hello";
// std::strlen - Calculates C-string length.
size_t len = std::strlen(buffer);
// std::strcmp - Compares C-strings lexicographically.
int cmp = std::strcmp(buffer, "Hello");
// std::strcat - Concatenates C-strings.
std::strcat(buffer, " World");
// std::strcpy - Copies source C-string to destination buffer.
char dest[20];
std::strcpy(dest, buffer);
return 0;
}
๐ง Core Concepts¶
- Low-Level C-Strings: C-strings are null-terminated character arrays (
char[]) requiring manual memory management and explicit boundary awareness to avoid overflows. - Standard String Helper Library: The
<cctype>and<cstring>libraries provide essential classification (e.g.,isalpha,isdigit) and buffer operations (e.g.,strlen,strcpy). - High-Level
std::stringandstd::string_view: C++ wraps raw buffers instd::string(managing dynamic resizing) andstd::string_view(providing lightweight, non-owning, read-only views).
โ ๏ธ Pitfalls (Quick Scan)¶
| Mistake | Fix |
|---|---|
Directly checking std::isalnum output against 1 |
Evaluate result in boolean contexts or cast using static_cast<bool> |
Passing a signed char with a negative value to <cctype> functions |
Cast char variables to unsigned char before passing them |
Applying sizeof to decayed character arrays (pointers) |
Use std::strlen to obtain actual character count excluding null terminator |
Incrementing the search pointer inside a std::strchr iteration loop |
Use a separate result pointer to track occurrences, and keep the source string read-only |
Calling std::strcat or std::strcpy with insufficient destination buffer size |
Ensure destination buffers are sized properly or use std::string instead |
Passing const char[] arrays as destination buffers to C-string modifiers |
Only pass non-const buffers to functions that modify string content |
Using operator += with integers on std::string to append numbers |
Convert numbers using std::to_string before appending |
Omitting manual null termination after calling std::strncpy |
Always append a null terminator (\0) manually to the end of the destination array |
Printing buffer returned by std::string::copy without null terminating |
Zero-initialize destination buffers or insert null terminators manually |
Concatenating two raw string literals with operator + |
Ensure at least one operand is a std::string or use adjacent literals without operators |
Comparing std::string to non-null-terminated character arrays |
Always ensure character arrays are null-terminated before comparing |
Allowing std::string_view to outlive the viewed string data |
Never use a std::string_view after its source string goes out of scope |
Calling std::strlen on a std::string_view data pointer after prefix/suffix modifications |
Use sv.length() or sv.size() instead of checking raw pointer length |
๐ Full Details¶
Cause โ Effect โ Fix with timestamp (click to expand)
* **Directly checking `std::isalnum` output against `1`** -> **Checks fail on some compilers because classification functions can return any non-zero value for true (05:54)** -> **Evaluate result in boolean contexts or cast using `static_cast๐ Repo Files¶
15.2CharacterManipulation/main.cpp15.3CStringManipulation/main.cpp15.4CStringConcatenationAndCopy/main.cpp15.6DeclaringAndUsingStdString/main.cpp15.7ConcatenatingStdStrings/main.cpp15.8AccessingCharactersInStdString/main.cpp15.9StdStringSizeAndCapacity/main.cpp15.10ModifyingStdStrings/main.cpp15.11ComparingStdStrings/main.cpp15.12StdStringCompare/main.cpp15.13StdStringReplacingCopyingResizingSwapping/main.cpp15.14SearchingStdString/main.cpp15.15TransformingStdStringFromToNumbers/main.cpp15.16EscapeSequences/main.cpp15.17RawStringLiterals/main.cpp15.18CopiedStrings/main.cpp15.19StringView/main.cpp