← C++ from Scratch
Lesson 11 of 62 · 10 XP
Strings: std::string
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
std::string (from <string>) is a string that looks after its own memory:
std::string s = "Hello";
s += ", IGD"; // join: "Hello, IGD"
s.size(); // 10 — the length (the same as length())
s[0]; // 'H' — a character by its number (from zero)
s.substr(7, 3); // "IGD" — from position 7, 3 characters
s.find("IGD"); // 7 — where it starts; if not found — std::string::npos
s.empty(); // whether it's empty
std::to_string(42); // "42" — a number to a string
std::stoi("42"); // 42 — a string to a number
for (char c : s) { … } // go through the characters
Functions for characters are in <cctype>: std::toupper(c), std::isdigit(c), std::isalpha(c).
Task. Read a word. Print its length, then the same word in upper case (Latin letters only), then the word backwards.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.