C++ String Operations
Find a delimiter before extracting two parts of a delivery label.
How it works
String searching returns a position or the special npos value when a match is absent. Check the result before arithmetic or slicing. A substring creates another owning string, so it remains independent of later edits to the original. The small grammar in this example is one colon separating a zone and a stop name. It is not a parser for arbitrary addresses, which may themselves contain punctuation. Define an input format before treating a delimiter search as complete validation.
Run it locally
Use a C++20 compiler and matching standard library in a separate lab folder. The command uses c++; substitute g++, clang++ or zig c++ for your toolchain. C++ does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
c++ -std=c++20 -Wall -Wextra -Wpedantic -Werror string-operations.cpp -o lessonOn Windows use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Thread examples may need -pthread on your platform.
Download the complete C++ source. Run file labs in a private empty directory, away from existing reports.
Example
#include <iostream>
#include <string>
int main() {
std::string label{"West:Gate"};
auto split = label.find(':');
if (split == std::string::npos) { return 1; }
std::string zone = label.substr(0, split);
std::string stop = label.substr(split + 1);
std::cout << zone << " zone, " << stop << " stop\n";
}
Expected result
West zone, Gate stop
The delimiter position is checked before adding one. substr copies the prefix and suffix into independent strings. The fixed fixture contains exactly one separator and two nonempty parts; a public parser should explicitly reject missing parts or unexpected extra separators. Value comparison with == compares string content, unlike comparing raw character-pointer addresses.
Consult the WG21 C++ working group and the C++ Core Guidelines for language references and design guidance. Check the selected standard version and your implementation’s library support.