String functions help us work with text easily. They let us change, check, and find parts of words or sentences.
String functions overview in C++
std::string str = "Hello"; // Common string functions: str.length(); // Get length of string str.empty(); // Check if string is empty str.append(" World"); // Add text at the end str.substr(pos, len); // Get part of string str.find("lo"); // Find position of substring str.compare(other); // Compare two strings str.erase(pos, len); // Remove part of string str.replace(pos, len, "new"); // Replace part of string std::transform(str.begin(), str.end(), str.begin(), ::toupper); // Convert to uppercase
All string functions are called on a string object using dot . notation.
Positions (pos) start at 0, meaning the first letter is at position 0.
std::string greeting = "Hello"; int len = greeting.length();
std::string text = "Hello"; text.append(" World");
part becomes "ell".std::string word = "Hello"; std::string part = word.substr(1, 3);
std::string word = "Hello"; int pos = word.find("lo");
This program shows common string functions: length, append, substring, find, replace, and converting to uppercase.
#include <iostream> #include <string> #include <algorithm> #include <cctype> int main() { std::string text = "Hello"; std::cout << "Original text: " << text << "\n"; // Length std::cout << "Length: " << text.length() << "\n"; // Append text.append(" World"); std::cout << "After append: " << text << "\n"; // Substring std::string part = text.substr(6, 5); std::cout << "Substring (6,5): " << part << "\n"; // Find size_t pos = text.find("World"); std::cout << "Position of 'World': " << pos << "\n"; // Replace text.replace(6, 5, "C++"); std::cout << "After replace: " << text << "\n"; // To uppercase std::transform(text.begin(), text.end(), text.begin(), ::toupper); std::cout << "Uppercase: " << text << "\n"; return 0; }
Remember string positions start at 0, so the first letter is position 0.
Functions like find return std::string::npos if the part is not found.
Use std::transform with care to change case; it needs <algorithm> and cctype headers.
String functions help us work with text easily.
Common tasks include finding length, adding text, cutting parts, and changing case.
Positions in strings start at zero, so count carefully.
Practice
Solution
Step 1: Understand string length function
Thelength()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 CQuick Check:
String length = length() [OK]
- Confusing length() with append()
- Using substr() to get length
- Thinking find() returns length
str in C++?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()andconcat()are not string methods in C++.push_back()adds a single character, not a string.Final Answer:
str.append(" world"); -> Option AQuick Check:
Use append() to add text [OK]
- Using add() or concat() which don't exist
- Using push_back() with string argument
- Missing semicolon at end
std::string s = "hello world"; std::cout << s.substr(6, 5);
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 DQuick Check:
substr(6,5) = "world" [OK]
- Starting count at 1 instead of 0
- Taking wrong number of characters
- Confusing substr with find
std::string s = "example"; s = s.substr(3, 10);
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 AQuick Check:
substr handles long length safely [OK]
- Expecting runtime error for long length
- Thinking start index is invalid
- Confusing substr arguments count
text = "Hello C++ World", which code snippet converts it to all uppercase letters?Solution
Step 1: Identify correct uppercase conversion
Looping over each character and applyingtoupper()converts all letters to uppercase.Step 2: Check other options
toUpperCase()andupper()are not C++ string methods. transform(text.begin(), text.end(), text.begin(), tolower); usestolowerwhich converts to lowercase.Final Answer:
for (auto &c : text) c = toupper(c); -> Option BQuick Check:
Use loop + toupper() for uppercase [OK]
- Using non-existent string methods
- Using tolower instead of toupper
- Not using reference in loop to modify string
Ready to prove it?
Pass the test and this pattern moves unknown
