WeCodeAcademy

C++ Strings

The std::string class is much friendlier than C strings.

ExampleC++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
  string s = "Hello, C++";
  cout << s.length() << endl;
  cout << s.substr(7) << endl;
  cout << s.find("C++") << endl;
  s += "!";
  cout << s << endl;

  string t = "banana";
  reverse(t.begin(), t.end());
  cout << t << endl;
  cout << (string("a") + "b" == "ab") << endl;
  return 0;
}
Output
10
C++
7
Hello, C++!
ananab
1
Quick check

Which member gives the number of characters in a std::string?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.