Parsing a string in C++

Level 387 keystrokes

A stringstream created from "10 20 30" can be read like cin. >> extracts the space-separated numbers into a, b and c, and their sum 60 is printed.

C++ code

// A stream whose input is the given text
1stringstream ss("10 20 30");
2int a, b, c;
// Each >> skips whitespace and converts the next number: a = 10, b = 20, c = 30
3ss >> a >> b >> c;
// Prints 60
4cout << a + b + c << endl;

Output

60
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.

  • >× 6
  • ;× 4
  • <× 4
  • ,× 2
  • "× 2
  • +× 2
  • (× 1
  • )× 1

More C++ examples at level 3

← C++ typing practice