Bird
Raised Fist0
C++programming~10 mins

String handling basics in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String handling basics
Start
↓
Declare string variable
↓
Assign value to string
↓
Access or modify string
↓
Use string functions
↓
Output or process string
↓
End
This flow shows how a string variable is declared, assigned, accessed or modified, and then used or output.
Execution Sample
C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Hello";
    s += " World";
    std::cout << s << std::endl;
    return 0;
}
This code creates a string, adds more text to it, and prints the result.
Execution Table
StepActionString sOutput
1Declare string s""
2Assign "Hello" to s"Hello"
3Append " World" to s"Hello World"
4Print s"Hello World"Hello World
5End of program"Hello World"Hello World
💡 Program ends after printing the final string.
Variable Tracker
VariableStartAfter 1After 2After 3Final
s"""""Hello""Hello World""Hello World"
Key Moments - 2 Insights
Why does the string s change after step 3?
Because in step 3, we use the += operator to add " World" to the existing string s, updating its value as shown in the execution_table row 3.
What is printed in step 4 and why?
Step 4 prints the current value of s, which is "Hello World" after the append operation, as shown in the Output column of execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of string s after step 2?
A""
B"Hello"
C"Hello World"
D"World"
💡 Hint
Check the 'String s' column in execution_table row 2.
At which step does the string s get its final value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at when s changes to "Hello World" in the execution_table.
If we remove step 3, what would be printed at step 4?
A"Hello"
B"Hello World"
C""
DCompilation error
💡 Hint
Without appending, s remains as assigned in step 2; see variable_tracker.
Concept Snapshot
String handling basics in C++:
- Declare with std::string s;
- Assign with s = "text";
- Append with s += "more";
- Access characters with s[index];
- Print with std::cout << s;
Strings are mutable and support many useful functions.
Full Transcript
This example shows how to handle strings in C++. First, we declare a string variable s. Then we assign it the value "Hello". Next, we add more text " World" to s using the += operator. Finally, we print the full string "Hello World". The execution table tracks each step, showing how s changes and what is output. Key moments clarify why s changes after appending and what is printed. The quiz tests understanding of string value changes and output. This teaches basic string creation, modification, and output in C++.

Practice

(1/5)
1. What does the following C++ code do?
std::string s = "Hello";
int len = s.length();
easy
A. Changes the string s to "length"
B. Stores the length of the string "Hello" in variable len
C. Prints the string length to the screen
D. Deletes the string s

Solution

  1. Step 1: Understand the string initialization

    The code creates a string variable s with the value "Hello".
  2. Step 2: Understand the length() function

    The length() function returns the number of characters in the string, which is 5 for "Hello".
  3. Final Answer:

    Stores the length of the string "Hello" in variable len -> Option B
  4. Quick Check:

    string length = 5 [OK]
Hint: length() returns number of characters in a string [OK]
Common Mistakes:
  • Thinking length() changes the string
  • Confusing length() with printing
  • Assuming length() deletes the string
2. Which of the following is the correct way to declare and initialize a string in C++?
easy
A. std::string s = "Hello";
B. string s = 'Hello';
C. char s = "Hello";
D. string s = Hello;

Solution

  1. Step 1: Check string declaration syntax

    In C++, strings are declared using std::string and initialized with double quotes "".
  2. Step 2: Analyze each option

    std::string s = "Hello"; uses std::string with double quotes correctly. string s = 'Hello'; uses single quotes which are for single characters. char s = "Hello"; uses char which holds one character, not a string. string s = Hello; misses quotes around Hello.
  3. Final Answer:

    std::string s = "Hello"; -> Option A
  4. Quick Check:

    Correct string syntax uses std::string and double quotes [OK]
Hint: Use std::string and double quotes for strings [OK]
Common Mistakes:
  • Using single quotes for strings
  • Using char for multiple characters
  • Forgetting quotes around text
3. What is the output of this C++ code?
std::string s = "Hello";
char c = s[1];
std::cout << c << std::endl;
medium
A. o
B. H
C. l
D. e

Solution

  1. Step 1: Understand string indexing

    Strings in C++ are zero-indexed, so s[0] = 'H', s[1] = 'e'.
  2. Step 2: Identify the character at index 1

    The code stores s[1] in c, which is 'e', then prints it.
  3. Final Answer:

    e -> Option D
  4. Quick Check:

    Index 1 character = 'e' [OK]
Hint: Remember string indexing starts at 0 [OK]
Common Mistakes:
  • Thinking indexing starts at 1
  • Confusing characters at different positions
  • Expecting the whole string instead of one char
4. Find the error in this code snippet:
std::string s = "Hello";
char c = s.at(5);
std::cout << c << std::endl;
medium
A. No error, prints 'o'
B. Compilation error: cannot assign char
C. Runtime error: out_of_range exception
D. Syntax error: wrong function name

Solution

  1. Step 1: Check string length and indexing

    The string "Hello" has length 5, valid indices are 0 to 4.
  2. Step 2: Understand s.at(5) behavior

    Accessing s.at(5) is out of range and throws a runtime exception.
  3. Final Answer:

    Runtime error: out_of_range exception -> Option C
  4. Quick Check:

    Accessing index 5 causes out_of_range error [OK]
Hint: s.at(index) throws error if index invalid [OK]
Common Mistakes:
  • Assuming s.at() works like s[] without error
  • Ignoring zero-based indexing
  • Confusing syntax and runtime errors
5. Given a string std::string s = "apple,banana,cherry";, which code correctly extracts the substring "banana"?
hard
A. std::string sub = s.substr(6, 6);
B. std::string sub = s.substr(5, 6);
C. std::string sub = s.substr(6, 7);
D. std::string sub = s.substr(7, 6);

Solution

  1. Step 1: Identify the start index of "banana"

    "apple," has 6 characters (indices 0 to 5), so 'b' starts at index 6.
  2. Step 2: Determine length of "banana"

    "banana" has 6 characters.
  3. Step 3: Use substr(start, length)

    Calling s.substr(6, 6) extracts 6 characters starting at index 6, which is "banana".
  4. Final Answer:

    std::string sub = s.substr(6, 6); -> Option A
  5. Quick Check:

    substr(6,6) = "banana" [OK]
Hint: Count start index and length carefully for substr() [OK]
Common Mistakes:
  • Off-by-one errors in start index
  • Wrong length parameter
  • Confusing substr parameters order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard