String Substring in C++

Level 263 keystrokes

Uses substr(0, 5) to take the first 5 characters of "Hello World" and prints Hello.

C++ code

// Stores "Hello World" in the string text
1string text = "Hello World";
// substr(pos, len) returns a new string of len characters starting at pos
// From index 0, 5 characters gives "Hello"; text itself is unchanged
2cout << text.substr(0, 5) << endl;

Output

Hello
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 4
  • ;× 2
  • "× 2
  • ,× 1
  • .× 1
  • (× 1
  • )× 1
  • =× 1

More C++ examples at level 2

← C++ typing practice