Difficulty: Easy
Correct Answer: strnset()
Explanation:
Introduction / Context:
Sometimes you need to initialize or overwrite a prefix of a string with a repeated character, such as filling with '' for masking. While the standard C function memset sets raw bytes, some environments provide string-oriented helpers. This question targets the function name commonly used for this task in certain libraries.
Given Data / Assumptions:
Concept / Approach:
strnset(s, ch, n) (non-standard, e.g., MSVC) sets the first n characters of s to ch and returns s. strset sets all characters, not just the first n. Functions like strinit and strcset are not standard. If strict portability is required, prefer memset(s, ch, n) while ensuring you do not overwrite the null terminator unintentionally.
Step-by-Step Solution:
Call strnset(s, ch, n) to set a prefix of the string.Ensure n does not exceed the string length prior to '\0' to preserve termination.Alternatively, use memset(s, ch, n) for portable code.Re-terminate if necessary to maintain a valid C string.
Verification / Alternative check:
After calling strnset(s, '', 3) on "hello", s becomes "***lo".
Why Other Options Are Wrong:
Common Pitfalls:
Accidentally overwriting the null terminator or using these non-standard functions in portable code. Prefer memset for cross-platform projects.
Final Answer:
strnset().
Discussion & Comments