All lessonsOpen Open Open Open Open
7. Strings
Strings
0 of 5 activities0%
Reading 1
Text as a type
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
What does string s = "hi"; cout << s.size(); print?
Fill in 3
Header
Try it 4
Build a greeting
Run it. Change the name and watch the length update.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Word length
Read one word into a string. Print how many characters it has, followed by a newline.
main.cpp
Loading editor…