📖 Scenario: Imagine you are working with a simple text editor that stores and processes words. Understanding how strings are stored and accessed in memory helps you manage text efficiently.
🎯 Goal: You will create a string variable, check its length, access characters by index, and print the string to see how it looks in memory representation.
📋 What You'll Learn
Create a string variable with a specific word
Create an integer variable to store the string length
Use a loop to access each character of the string by index
Print the string and its characters one by one
💡 Why This Matters
🌍 Real World
Text editors, word processors, and many software use strings to store and manipulate text data.
💼 Career
Understanding string basics is essential for programming tasks like parsing input, formatting output, and handling user data.
Progress0 / 4 steps
1
Create a string variable
Create a std::string variable called word and set it to the exact value "hello".
DSA C++
Hint
Use std::string to create the variable and assign the word "hello" exactly.
2
Create a length variable
Add an int variable called length and set it to the length of word using word.length().
DSA C++
Hint
Use word.length() to get the number of characters in the string.
3
Access characters by index
Write a for loop using int i = 0; i < length; i++ to access each character of word by index word[i].
DSA C++
Hint
Use a for loop from 0 to length - 1 and access characters with word[i].
4
Print the string and its characters
Print the full word using std::cout. Then inside the loop, print each character c on a new line.
DSA C++
Hint
Use std::cout << word << std::endl; to print the full string, then print each character inside the loop with std::cout << c << std::endl;.
Practice
(1/5)
1. What does the following C++ code represent? char str[] = "hello";
easy
A. An array of characters ending with a null character
B. A pointer to a constant string literal
C. A string object from the C++ standard library
D. A dynamically allocated string
Solution
Step 1: Understand the declaration type
The code declares str as a character array initialized with the string "hello".
Step 2: Recognize the null terminator in C-style strings
In C++, character arrays initialized with string literals automatically include a null character '\0' at the end to mark the string's end.
Final Answer:
An array of characters ending with a null character -> Option A
Quick Check:
C-style string = char array + '\0' [OK]
Hint: Remember C strings always end with '\0' null character [OK]
Common Mistakes:
Confusing array with pointer
Forgetting the null terminator
Thinking it's a C++ string object
2. Which of the following is the correct way to declare a string literal pointer in C++?
easy
A. char *str = "hello";
B. char str = "hello";
C. char str[] = 'hello';
D. string str = 'hello';
Solution
Step 1: Check pointer declaration syntax
char *str = "hello"; declares a pointer to a string literal correctly.
Step 2: Identify errors in other options
char str = "hello"; uses a single char variable for a string literal, which is invalid. char str[] = 'hello'; uses single quotes for multiple characters, which is invalid. string str = 'hello'; uses single quotes with string type, which requires double quotes.
Final Answer:
char *str = "hello"; -> Option A
Quick Check:
Pointer to string literal uses char * and double quotes [OK]
Hint: String literals need char* with double quotes [OK]