VLSI DV Interview Puzzles · All levels
putc/getc Index Semantics
After one character replacement, what are final string, length, and ASCII code?
Puzzle
Difficulty: Medium · Puzzle 2 of 6 · Topic: String and Casting Puzzles
After one character replacement, what are final string, length, and ASCII code?
Code
systemverilog
module p2;
string s = "dv puzzle";
int ch;
initial begin
s.putc(3, 8'h5f); // '_'
ch = s.getc(4);
$display("s=%s len=%0d ch=%0d", s, s.len(), ch);
end
endmoduleHint
String indexing is zero-based and `putc` writes at an index, it does not insert and shift.
Step-by-step solution
diagram
1) Original index 3 is character `p` in `"dv puzzle"`.
2) `putc(3,'_')` replaces `p`, giving `"dv _uzzle"`.
3) Length remains 9 because replacement is in-place.
4) Index 4 is `u`, whose ASCII code is 117.Answer
Answer: s=dv _uzzle, len=9, ch=117
Why candidates get it wrong
Many candidates treat `putc` like insertion and incorrectly change length/index mapping.
Interviewer follow-up
What does `s.getc(100)` return for out-of-range index?