It returns the number of characters in the string, excluding the null terminator.
Click to reveal answer
beginner
How do you convert a std::string to a C-style string?
Use the c_str() function, which returns a pointer to a null-terminated character array.
Click to reveal answer
intermediate
What is the purpose of std::string::substr()?
It extracts a substring from the string, starting at a given position and optionally for a given length.
Click to reveal answer
intermediate
How does std::string::find() work?
It searches for a substring or character inside the string and returns the position of the first occurrence or std::string::npos if not found.
Click to reveal answer
beginner
What does std::string::append() do?
It adds characters or another string to the end of the current string, increasing its length.
Click to reveal answer
Which function returns the length of a std::string?
Astrlen()
Bsize()
Ccount()
Dlength()
✗ Incorrect
length() and size() both return the string length in C++, but length() is the most common for strings. strlen() is for C-style strings.
What does std::string::find() return if the substring is not found?
A<code>std::string::npos</code>
B-1
C0
DThrows an exception
✗ Incorrect
If the substring is not found, find() returns std::string::npos, a special constant indicating no match.
Which function would you use to get a part of a string starting at position 3 with length 5?
Aslice(3, 5)
Bsubstring(3, 5)
Csubstr(3, 5)
Dcut(3, 5)
✗ Incorrect
substr() extracts a substring from a string starting at the given position and length.
How do you add "world" to the end of a string s?
As.push_back("world")
Bs.add("world")
Cs.insert("world")
Ds.append("world")
✗ Incorrect
append() adds characters or strings to the end of a std::string.
Which function returns a pointer to a C-style string from a std::string?
Ac_str()
Bdata()
Cto_cstring()
Dptr()
✗ Incorrect
c_str() returns a pointer to a null-terminated C-style string.
Explain how to find a substring inside a std::string and handle the case when it is not found.
Think about the function that searches and what it returns if no match.
You got /4 concepts.
Describe how to extract a part of a string and how to add text to the end of a string in C++.
Two common string operations: cutting and joining.
You got /5 concepts.
Practice
(1/5)
1. Which C++ string function returns the number of characters in a string?
easy
A. substr()
B. append()
C. length()
D. find()
Solution
Step 1: Understand string length function
The length() function returns how many characters are in the string.
Step 2: Compare with other functions
append() adds text, substr() extracts part, find() searches for text.
Final Answer:
length() -> Option C
Quick Check:
String length = length() [OK]
Hint: Length means count characters, use length() [OK]
Common Mistakes:
Confusing length() with append()
Using substr() to get length
Thinking find() returns length
2. Which of the following is the correct syntax to add " world" to a string str in C++?
easy
A. str.append(" world");
B. str.add(" world");
C. str.concat(" world");
D. str.push_back(" world");
Solution
Step 1: Identify correct append syntax
In C++, append() adds text to the end of a string: str.append(" world");.
Step 2: Check other options
add() and concat() are not string methods in C++. push_back() adds a single character, not a string.
Final Answer:
str.append(" world"); -> Option A
Quick Check:
Use append() to add text [OK]
Hint: Use append() to add text, not add() or concat() [OK]
Common Mistakes:
Using add() or concat() which don't exist
Using push_back() with string argument
Missing semicolon at end
3. What is the output of this C++ code?
std::string s = "hello world";
std::cout << s.substr(6, 5);
medium
A. worlds
B. hello
C. hello world
D. world
Solution
Step 1: Understand substr parameters
substr(6, 5) means start at index 6 and take 5 characters.
Step 2: Extract substring from "hello world"
Index 6 is 'w', next 5 characters are "world".
Final Answer:
world -> Option D
Quick Check:
substr(6,5) = "world" [OK]
Hint: Count from zero, substr(start, length) extracts text [OK]
Common Mistakes:
Starting count at 1 instead of 0
Taking wrong number of characters
Confusing substr with find
4. Identify the error in this C++ code snippet:
std::string s = "example";
s = s.substr(3, 10);
medium
A. substr length exceeds string length but no error occurs
B. substr start index is invalid
C. substr requires two arguments but only one given
D. Cannot assign substr result to string variable
Solution
Step 1: Check substr parameters
Start index 3 is valid. Length 10 is longer than remaining string length.
Step 2: Understand substr behavior with long length
substr returns substring from index 3 to end without error even if length is too big.
Final Answer:
substr length exceeds string length but no error occurs -> Option A
Quick Check:
substr handles long length safely [OK]
Hint: substr length can be longer than string safely [OK]
Common Mistakes:
Expecting runtime error for long length
Thinking start index is invalid
Confusing substr arguments count
5. Given a string text = "Hello C++ World", which code snippet converts it to all uppercase letters?
hard
A. text.toUpperCase();
B. for (auto &c : text) c = toupper(c);
C. text.upper();
D. transform(text.begin(), text.end(), text.begin(), tolower);
Solution
Step 1: Identify correct uppercase conversion
Looping over each character and applying toupper() converts all letters to uppercase.
Step 2: Check other options
toUpperCase() and upper() are not C++ string methods. transform(text.begin(), text.end(), text.begin(), tolower); uses tolower which converts to lowercase.
Final Answer:
for (auto &c : text) c = toupper(c); -> Option B
Quick Check:
Use loop + toupper() for uppercase [OK]
Hint: Use loop with toupper() to change case [OK]
Common Mistakes:
Using non-existent string methods
Using tolower instead of toupper
Not using reference in loop to modify string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.