Difficulty: Easy
Correct Answer: The sentence will get printed in reverse order
Explanation:
Introduction / Context:
This question verifies knowledge of iterating backwards through a C string using strlen and array indexing. The code demonstrates reversal by printing characters from the last index down to zero.
Given Data / Assumptions:
Concept / Approach:
The loop initializes i to strlen(sentence) - 1 (the last valid character index) and decrements down to 0 inclusive. This prints the string in reverse order.
Step-by-Step Solution:
User enters text, e.g., "hello".strlen("hello") = 5, so start i = 4.putchar(sentence[4]) prints 'o', then i becomes 3 → prints 'l', then 2 → 'l', then 1 → 'e', then 0 → 'h'.Loop ends after i becomes -1, and the output is the reverse of the input.
Verification / Alternative check:
Compare with using strrev (non-standard) or manual two-pointer reversal in a buffer; all produce the reversed text.
Why Other Options Are Wrong:
Same order: would require forward iteration.Half printed: the loop clearly covers all indices down to 0.None of above / does not compile: the code compiles; logical behavior is reversal.
Common Pitfalls:
Using gets (unsafe, buffer overflow risk); forgetting that strlen excludes the null terminator; off-by-one errors in start or end indices.
Final Answer:
The sentence will get printed in reverse order
Discussion & Comments