All lessons

7. Strings

Strings

0 of 5 activities0%

Reading 1

Text as a type

Open

Include <string> and use string for text. You can read a whole word with cin, or a full line with getline.

s.size() (or s.length()) tells you how many characters are in the string. You can index characters with s[i], starting at 0.

Two strings can be joined with +.

#include <string>

string name = "Ada";
cout << name.size() << endl;   // 3
cout << name[0] << endl;       // A
cout << name + " Lovelace" << endl;

Check 2

Length

Open

What does string s = "hi"; cout << s.size(); print?

Fill in 3

Header

Open

The header you include for std::string is

Try it 4

Build a greeting

Open

Run it. Change the name and watch the length update.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Word length

Open

Read one word into a string. Print how many characters it has, followed by a newline.

main.cpp
Loading editor…