📖 Scenario: You are working on a simple program that stores and manipulates a name using character arrays in C++. This is like writing a name on a piece of paper where each letter is stored in order.
🎯 Goal: Build a program that creates a character array with a name, counts how many letters it has, and then prints the name and its length.
📋 What You'll Learn
Create a character array with the exact name "Alice"
Create an integer variable to store the length of the name
Use a loop to count the number of characters in the array until the end
Print the name and the length
💡 Why This Matters
🌍 Real World
Character arrays are used in many programs to store and manipulate text, especially in older or low-level C++ code.
💼 Career
Understanding character arrays helps in jobs involving embedded systems, game development, or any C++ programming where direct memory control is important.
Progress0 / 4 steps
1
Create a character array
Create a character array called name and set it to the exact string "Alice".
C++
Hint
Use double quotes to set the string inside the character array.
2
Create a length variable
Create an integer variable called length and set it to 0.
C++
Hint
This variable will count how many letters are in the name.
3
Count characters in the array
Use a for loop with an integer i starting at 0 to count characters in name until you find the null character '\0'. Increase length by 1 for each character.
C++
Hint
Remember, character arrays end with a special '\0' character.
4
Print the name and length
Use std::cout to print the text Name: followed by name, then print Length: followed by length. Each on its own line.
C++
Hint
Use std::cout with << to print text and variables.
Practice
(1/5)
1. What is the purpose of the null terminator '\0' in a character array in C++?
easy
A. It marks the end of the string stored in the array.
B. It initializes all characters to zero.
C. It increases the size of the array by one.
D. It converts characters to uppercase.
Solution
Step 1: Understand the role of '\0' in character arrays
The null terminator '\0' is a special character used to mark the end of a string in C++ character arrays.
Step 2: Identify its effect on string handling
Functions that work with strings stop reading characters when they encounter '\0', so it tells where the string ends.
Final Answer:
It marks the end of the string stored in the array. -> Option A
Quick Check:
Null terminator = end of string [OK]
Hint: Remember '\0' ends the string, like a period ends a sentence. [OK]
Common Mistakes:
Thinking '\0' initializes characters to zero
Confusing '\0' with increasing array size
Assuming '\0' changes character case
2. Which of the following is the correct way to declare a character array to hold the word "code" including the null terminator?
easy
A. char word[3] = {'c', 'o', 'd', '\0'};
B. char word[4] = {'c', 'o', 'd', 'e'};
C. char word[4] = "code";
D. char word[5] = {'c', 'o', 'd', 'e', '\0'};
Solution
Step 1: Count characters and null terminator
The word "code" has 4 letters plus 1 null terminator '\0', so the array size must be 5.
Step 2: Check each option's array size and initialization
char word[5] = {'c', 'o', 'd', 'e', '\0'}; correctly declares a char array of size 5 and includes the null terminator explicitly.