C++ String Methods
This page keeps the seven lines from the strings page and adds two lines under them. The strings page ended with pine, 2, 0.5, 1, 4, pine cone, and p. That seven-line screen is the step this page builds on. The first box still holds the word pine, 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 boxes, and its build command, so the example changes only by two new writing lines. Both new lines call functions that belong to the joined box. One function finds where a word starts. The other copies a piece of the text. Both calls are members of std::string, so they use the dot after the box name, the same way size() works on word.
Search and copy are two of the most common text jobs in any program. A login form checks an email address for an at sign, a log reader looks for an error code, and a file name is cut before its extension. This page gives you the two tools for those jobs in one small file, so the pattern is clear before the longer examples arrive.
The function find looks for a piece of text inside a std::string. The call joined.find("cone") starts at the first letter of pine cone and checks each position until it reaches the word cone. It returns the index where the match begins. The index counts from zero, so p sits at 0, i at 1, n at 2, e at 3, the space at 4, and the c of cone at 5. The result is 5, and cout prints it as a plain number.
find returns a position, not text. A position is a count of steps from the start, and it is a different kind of value from the word that was searched for. You can store the result in a variable and use it as an index later. If the word is missing from the string, find returns a special value called std::string::npos. That value is a very large number, and it is never a valid index. This page avoids a missing word, so the screen stays predictable. When you write search code later, check the result against npos before you use it as a position.
The function substr copies a piece of a string and returns the copy as new text. The call joined.substr(5, 4) starts at index 5, which is the c of cone, and copies 4 characters. The copy is cone. The first number says where to start. The second number says how many characters to copy. The count of characters in pine cone is 9, so the last index is 8. The copy from index 5 with length 4 ends at index 8, which is still inside the text.
If you leave out the second number, substr copies from the start position to the end of the text. The call joined.substr(5) gives cone as well, because nothing comes after the e. A start position past the end of the text is a different case. It makes substr throw an error called std::out_of_range, and this page keeps the start inside the text. The copy returned by substr is a new string, so joined is not changed. The screen only shows the copy, and the box keeps the full text.
Both calls return a value, so they belong inside cout the same way word and count do. They do not change the box they read. After find and substr run, joined still holds pine cone with the space in the middle. That is the rule for this group of functions: each one reads the box and returns an answer, and the box stays as it was unless the function's name says it changes the text.
A quick way to keep that rule in mind is to ask two questions before each line: where does the answer come from, and does the box change? For find and substr, the answer comes from joined, and the box stays the same. Asking those questions keeps your code from changing a value by accident.
Type the file in this order, or add only the two new lines if types.cpp is already on disk. Keep the seven lines from the strings page. Under the word[0] writing line, add two lines: one for find and one for substr. 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;
std::cout << joined.find("cone") << std::endl;
std::cout << joined.substr(5, 4) << 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 nine lines: pine, 2, 0.5, 1, 4, pine cone, p, 5, and cone. The first seven lines are the same as on the strings page. The eighth line is the index from find, and the ninth line is the copy from substr.
Check the eighth line first. The index of cone in pine cone is 5, so the screen shows 5 and not 4 or 6. Then check the ninth line. The copy is exactly four letters, cone, with no space and no extra letter. The table gives each row its piece, so compare your run with it line by line. If the screen has more than nine lines, check that no writing line was copied twice.
📊 Two string methods and what they show
| Piece | What the screen shows |
|---|---|
| Find | 5, the index where cone starts in pine cone |
| Cut | cone, the four letters from index 5 by substr(5, 4) |
| Unchanged | pine cone, joined still holds the full text |
| Screen | pine, 2, 0.5, 1, 4, pine cone, p, 5, cone |
Change the start in the substr call to 0 and the length to 4, then rebuild. The ninth line becomes pine, because the copy starts at the first letter. Put the start back to 5. Next, change find("cone") to find("pine"). The eighth line becomes 0, because pine starts at the first letter of pine cone. Put the search word back, and the line returns to 5. Last, delete the second number in the substr call, so the line reads joined.substr(5). The ninth line still shows cone, which confirms that the second number was optional. Put the length back before you leave.
The next page is numbers. Its job is to use int and double values in arithmetic, such as addition, subtraction, multiplication, and division, in the same types.cpp file. This page stops before any arithmetic. You leave with nine lines on the screen, the index 5 from find, and the copy cone from substr. Count the lines before you leave. There are nine, and the last one is cone.
🧠 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