C++ Strings
This page keeps the four lines from the types page and adds three lines under them. The types page ended with pine, 2, 0.5, and 1, one line for each box. That four-line screen is the step this page builds on. The first box still holds the word pine as a std::string, and the comment // the word stays pine still sits above the declarations.
This module stays on the same file. types.cpp keeps its name, its four boxes, and its build command, so the example changes only by three new writing lines and two new declarations.
A std::string is a box for text. It has functions that belong to the box, and you call them with a dot after the name. The call word.size() asks the box how many characters it holds. The count includes every letter in pine. The result of size() is a count, not text, so cout prints it as a plain number. The count starts from zero for an empty box. A box that holds nothing, such as a std::string with no value, reports 0, and a box that holds a single letter reports 1. The count is about characters in the box, so the quotes around pine are not counted and the letters inside them are. The word is four letters long, and that is what the screen shows.
The name size is the usual choice in modern C++, and length does the same job for std::string. Both names work, and this page uses size so the line reads the same way as the rest of the standard library examples you will see later.
Two strings join with the plus sign. The expression word + " " + other reads the box word, then a single space written in quotes, then the box other, and it makes one new string with all three parts. The space in the quotes is a character too, so the joined text is pine cone with one space between the words. The joined text goes into its own box, and that box can be printed the same way as word. Joining does not change word or other. The joined box is a new copy with all the characters in order, so later changes to word would not reach joined unless the join line runs again. Read the lines in order from the top, because each line uses the boxes as they stand at that point in the file. The space is one character, and it sits between the two words because the quoted space sits between the two boxes in the expression.
Reading one letter uses square brackets and an index. The expression word[0] reads the first letter of word, and the index starts at 0, not 1. So word[0] is p, word[1] is i, word[2] is n, and word[3] is e. The index must stay inside the text. An index of 4 on the four-letter word reads past the end, which is a mistake this page does not make. The last valid index for pine is 3, and that is one less than the length. Counting from zero feels odd at first, but the rule stays the same for every string: the first letter is at 0, and the last letter is at the length minus one. A reader who keeps that rule in mind can check any index by hand before running the file.
Type the file in this order, or add only the new lines if types.cpp is already on disk. Keep the four type declarations and their four writing lines. Under the bool writing line, add the string box other, the joined box, and three writing lines: one for size, one for joined, and one for the first letter. The checked file is below. The file name on disk stays types.cpp.
#include <iostream>
#include <string>
int main()
{
// the word stays pine
std::string word = "pine";
int count = 2;
double ratio = 0.5;
bool ready = true;
std::string other = "cone";
std::string joined = word + " " + other;
std::cout << word << std::endl;
std::cout << count << std::endl;
std::cout << ratio << std::endl;
std::cout << ready << std::endl;
std::cout << word.size() << std::endl;
std::cout << joined << std::endl;
std::cout << word[0] << std::endl;
return 0;
}
Rebuild with g++ -std=c++17 types.cpp -o types and then run ./types. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 types.cpp -o types. The screen shows seven lines: pine, 2, 0.5, 1, 4, pine cone, and p. The first four lines are the same as on the types page. The fifth line is the count of word, the sixth is the joined text, and the seventh is the first letter.
Check the fifth line first. The word pine has four letters, so the screen shows 4. Then check the sixth line for one space between pine and cone. The seventh line is one letter, p, and not the whole word. The table gives each row its piece, so compare your run with it line by line. If the screen has more than seven lines, check that no writing line was copied twice.
📊 Three string lines and what they show
| Piece | What the screen shows |
|---|---|
| Length | 4, the number of characters in word |
| Join | pine cone, word plus one space plus other |
| Letter | p, the letter at index 0 of word |
| Screen | pine, 2, 0.5, 1, 4, pine cone, p |
Change the index to word[2] and rebuild. The seventh line becomes n, because index 2 is the third letter. Put the index back to 0 and the line returns to p. Next, delete the plus sign between word and the space in the join line. The compiler stops with an error, because a string and a quoted space must be joined with the plus sign. Put the plus sign back. Each string line gives one result you can explain from the type and the index. Try one more change: replace the quoted space in the join line with an underscore, so the expression reads word + "_" + other, then rebuild. The sixth line shows pine_cone, and the count and the first letter stay the same. Put the space back, and the sixth line returns to pine cone.
The next page is string methods. Its job is to call more functions on text, such as find, which looks for a word inside a string, and substr, which copies part of one. This page stops before any of those functions. You leave with the seven lines on the screen, the count of 4 for word, and no search call yet. Count the lines before you leave. There are seven, and the seventh line is p.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top