Recall & Review
beginner
What is string traversal in C?
String traversal in C means going through each character of a string one by one, usually using a loop, until the end of the string is reached.
Click to reveal answer
beginner
How do you access the third character of a string named
str in C?You access it using
str[2] because indexing starts at 0, so the first character is str[0], second is str[1], and third is str[2].Click to reveal answer
beginner
What marks the end of a string in C?
A string in C ends with a special character called the null character, written as
'\0'. It tells the program where the string stops.Click to reveal answer
intermediate
Why is it important to check for the null character during string traversal?
Because the null character
'\0' marks the end of the string. If you don't check for it, you might read beyond the string and get wrong data or cause errors.Click to reveal answer
beginner
Show a simple C code snippet to print each character of a string
str on a new line.#include
int main() {
char str[] = "hello";
int i = 0;
while (str[i] != '\0') {
printf("%c\n", str[i]);
i++;
}
return 0;
}
Click to reveal answer
What does the expression
str[i] represent in C when str is a string?✗ Incorrect
str[i] accesses the character at index i in the string str.
Which character indicates the end of a string in C?
✗ Incorrect
The null character '\0' marks the end of a string in C.
What will happen if you forget to check for the null character while traversing a string in C?
✗ Incorrect
Without checking for '\0', traversal can go past the string and read invalid memory.
If
char str[] = "abc";, what is the value of str[3]?✗ Incorrect
The string "abc" is stored as 'a', 'b', 'c', '\0'. So str[3] is the null character.
Which loop is commonly used to traverse a string until its end in C?
✗ Incorrect
A while loop checking for str[i] != '\0' is common for string traversal.
Explain how to traverse a string in C and access each character safely.
Think about how to stop at the end of the string.
You got /3 concepts.
Describe why the null character is important in C strings and what could happen if it is ignored during traversal.
Consider what tells the program where the string ends.
You got /3 concepts.
