String Functions Part 1
Part of the Fundamentals section of Coddy's C++ journey — lesson 70 of 74.
Here are some useful functions in strings:
insert(pos, str): Inserts the stringstrat positionposin the current string.
replace(pos, len, str): Replaceslencharacters starting at positionposwith the stringstr.
substr(pos, len): Returns a substring of the current string, starting at positionposand having lengthlen.
append(str): Adds the stringstrto the end of the current string.
For example using the following string:
std::string str = "Hello, World!";str.insert(5, " C++");
// Output: "Hello C++, World!"str.replace(7, 5, "Universe");
// Output: "Hello, Universe!"str.substr(0, 5);
// Output: "Hello"str.substr(7, 5);
// Output: "World"str.substr(7);
// Output: "World!"str.append(" This is universe");
// Output: "Hello, World! This is universe"Challenge
EasyCreate a function named stringOperations that takes a string as input and performs the following operations using string functions:
- Prints the length of the string.
- Appends the string
" - Modified"to the original string. - Inserts the string
"C++ "at the beginning of the string. - Extracts a substring of length 5 starting at position 5.
- Replaces the occurrence of the substring of length 5 start at position 5 with the string "Awesome".
- Prints the modified string after each operation.
Use the following format:
Length: [Length of string]
Append: [After Appending]
Insert: [After Insert]
Extract: [The extracted string]
Replace: [After replacing]Cheat sheet
Useful C++ string functions:
insert(pos, str): Inserts stringstrat positionposreplace(pos, len, str): Replaceslencharacters starting at positionposwith stringstrsubstr(pos, len): Returns substring starting at positionposwith lengthlenappend(str): Adds stringstrto the end
std::string str = "Hello, World!";
str.insert(5, " C++");
// "Hello C++, World!"
str.replace(7, 5, "Universe");
// "Hello, Universe!"
str.substr(0, 5);
// "Hello"
str.append(" This is universe");
// "Hello, World! This is universe"Try it yourself
#include <iostream>
#include <string>
void stringOperations(std::string str) {
// 1. Print length of the string
// 2. Append " - Modified" to the string
// 3. Insert "C++ " at the beginning
// 4. Extract substring of length 5 starting at position 5
// 5. Replace substring at position 5 with "Awesome"
}
int main() {
std::string str;
std::getline(std::cin, str);
stringOperations(str);
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else9Loops
For Loop Part 1While LoopDo While LoopBreakContinueFor Loop Part 2Nested LoopsInfinite LoopsRecap - Dynamic Input12Strings
C-style Strings Part 1C-style Strings Part 2String OperationsString Functions Part 1String Functions Part 2