← Back to problems
UNSW COMP1521 · hard

COMP1521 2020T2 Q8 — Character encoding (UTF-8)

COMP1521 2020T2 Q8 · 10 marks · C Topic: Character encoding (UTF-8) · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

Summary: Count and validate the UTF-8 characters in a file

We need to count the number of UTF-8 characters in a file, and check that the file contains only valid UTF-8.

Write a C program, practice_q8.c , which takes a single filename as its argument, counts the number of UTF-8 characters in the named file, and prints one line of output containing that count.

Use the same format as the example below.

dcc practice_q8.c -o practice_q8 echo hello world >file1 ./practice_q8 file1 file1: 12 UTF-8 characters echo -e -n '\xF0\x90\x8D\x88' >file2 ./practice_q8 file2 file2: 1 UTF-8 characters echo -e -n '\x24\xC2\xA2\xE0\xA4\xB9' >file3 ./practice_q8 file3 file3: 3 UTF-8 characters ./practice_q8 utf8.html utf8.html: 7943 UTF-8 characters

If practice_q8.c reads a byte which is not valid UTF-8, it should stop and print an error message in the same format as the example below. Note the error message includes how many valid UTF-8 characters have been previously read.

echo -e -n '\x24\xC2\xA2\xE0\xA4\x09' >file4 ./practice_q8 file4 file4: invalid UTF-8 after 2 valid UTF-8 characters

If the end of file is reached before the completion of a UTF-8 character, practice_q8.c should also print an error message; for example:

echo -e -n '\x24\xC2\xA2\xE0\xA4' >file5 ./practice_q8 file5 file5: invalid UTF-8 after 2 valid UTF-8 characters

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

There is no supplied code for this question.

Only the specified error checking is required; no additional error checking is required.

Your program can assume it is always given a single argument, the name of a file.

Your solution must be in C only.

You are not permitted to run external programs. You are not permitted to use system, popen, posix_spawn, fork or exec.


🤖 Write a C program. argv[1] is a filename. Count the UTF-8 characters; if you hit an invalid sequence (bad lead byte, bad continuation, or EOF mid-character) stop and print <filename>: invalid UTF-8 after <N> valid UTF-8 characters; otherwise print <filename>: <K> UTF-8 characters.

Verified on the sandbox, cross-checked against an independent decoder, and reproduces every official example (raw-byte fixtures).