String Empty Check in C++

Level 286 keystrokes

Checks with empty() whether the string s has no characters; s is "", so Empty is printed.

C++ code

// Creates an empty string (length 0)
1string s = "";
// empty() returns true when the string has no characters
2if (s.empty()) {
// s is empty, so Empty is printed
3 cout << "Empty" << endl;
// Runs only if s contains at least one character
4} else {
// Would print the contents of s; skipped here
5 cout << s << endl;
6}

Output

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

  • <× 8
  • "× 4
  • ;× 3
  • (× 2
  • )× 2
  • {× 2
  • }× 2
  • .× 1
  • =× 1

More C++ examples at level 2

← C++ typing practice