COMP1521 2022T3 Q7 · 10 marks · C 知识点: 字符编码(UTF8) · 次: 字符串/校验/替换 · 难度: 进阶 Topic: Character encoding (UTF-8) · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html
一句话 / Summary: 把非法UTF-8序列替换为问号
You have been given 22t3final_q7.c , which contains a C function _22t3final_q7 , that takes a string, and returns it unmodified.
Add code to the function _22t3final_q7 so that, given a UTF-8 encoded string, it returns a new string that has any invalid UTF-8 sequences replaced with the character '?'.
For example:
xxd 22t3final_q7_examples/hello_world.txt 00000000: 6865 6c6c 6f20 776f 726c 64 hello world ./22t3final_q7 < 22t3final_q7_examples/hello_world.txt hello world xxd 22t3final_q7_examples/bad_hello_world.txt 00000000: 6865 6c6c 6fa1 776f 726c 64 hello.world ./22t3final_q7 < 22t3final_q7_examples/bad_hello_world.txt hello?world xxd 22t3final_q7_examples/valid_with_emoji.txt 00000000: 796f 7520 6172 6520 646f 696e 6720 6772 you are doing gr 00000010: 6561 7420 f09f 918d f09f 918d f09f 918d eat ............ ./22t3final_q7 < 22t3final_q7_examples/valid_with_emoji.txt you are doing great 👍👍👍 xxd 22t3final_q7_examples/invalid_with_emoji.txt 00000000: 7468 6973 f0a1 3923 7374 7269 6e67 a269 this..9#string.i 00000010: 73e7 8936 6e6f 74a0 646f 696e 67c2 c273 s..6not.doing..s 00000020: 6fa0 6772 6561 74f1 3242 a068 6f77 6576 o.great.2B.howev 00000030: 6572 20f0 9f98 ad91 er ..... ./22t3final_q7 < 22t3final_q7_examples/invalid_with_emoji.txt this?string?is?not?doing?so?great?however 😭?
Further example inputs are provided in the 22t3final_q7_examples/ directory.
You can use make(1) to build your code:
make 22t3final_q7
A reminder of how UTF-8 is encoded:
#bytes #bits Byte 1 Byte 2 Byte 3 Byte 4
1 7 0xxxxxxx
2 11 110xxxxx 10xxxxxx
3 16 1110xxxx 10xxxxxx 10xxxxxx
4 21 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
The provided string may be of any length.
The provided string will not contain any null bytes, apart from the terminating null byte.
Each occurrence of a continuation byte outside a sequence should be replaced with a '?'.
If a valid starting byte occurs within another UTF-8 sequence, it should be considered part of that sequence, thus rendering the sequence invalid. It should not be considered the start of a new sequence.
22t3final_q7_main.c includes a main function to help you test your code.
You may not change the provided 22t3final_q7_main.c file.
You may not change _22t3final_q7 's prototype.
ie: its return type and its argument types.
You may define and call your own functions if you wish.
Your solution must be in C only.
Your solution must not be in MIPS , C++ , Python , or any other language.
Your program cannot call external programs.
Your program cannot use any of: fork() / exec() , posix_spawn() , system() , or any other similar functions.
Your program may call any functions from the C standard library available by default on CSE Linux systems.
Including but not limited to: stdio.h , stdlib.h , string.h , math.h , assert.h , etc.
🤖 Adapted: write a C program.
argv[1]is a filename (delivered as raw bytes). Copy it to stdout, replacing every invalid UTF-8 sequence with a single?(a bad lead byte consumes 1 byte; a broken/truncated multi-byte sequence consumes its expected length).Verified on the sandbox, cross-checked against an independent decoder, and reproduces every official example (raw-byte fixtures).