COMP1521 2022T2 Q6 — Character encoding (UTF-8)
COMP1521 2022T2 Q6 · 10 marks · C
Topic: Character encoding (UTF-8) · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html
Summary: Extract a UTF-8 substring by code-point range
You have been given 22t2final_q6.c ,
which contains a C function 22t2final_q6 ,
that takes three parameters...
char *utf8_string : a UTF-8 encoded string,
unsigned int range_start : the (inclusive) starting index,
unsigned int range_end : the (exclusive) ending index.
... and returns a char * .
#include <string.h>
#include <stdlib.h>
/**
- given a
UTF-8encoded string, - return a new string that is only
- the characters within the provided range.
*
- Note:
range_startis INCLUSIVErange_endis EXCLUSIVE
*
- eg:
- "hello world", 0, 5
- would return "hello"
*
- "🍓🍇🍈🍍🍐", 2, 5
- would return "🍈🍍🍐"
**/
char _22t2final_q6 ( char utf8_string , unsigned int range_start , unsigned int range_end ) {
char * new_string = strdup ( utf8_string );
return new_string ;
}
Add code to the function 22t2final_q6 so that,
given the above parameters,
it returns a new string comprised of
the UTF-8 code-points that lie in the range
of range_start to range_end
in the provided utf8_string .
Note that the returned string must be a new string;
i.e. you must not modify the provided
utf8_string -- you must instead
use malloc (or otherwise, such as strdup )
to allocate new memory that you can then return.
main will later free that memory
for you.
For example:
make 22t2final_q6
./22t2final_q6 "hello world" 3 8
22t2final_q6("hello world", 3, 8) returned "lo wo"
./22t2final_q6 "🍓🍇🍈🍍🍐🍖🍧🍆🍊🍞" 2 4
22t2final_q6("🍓🍇🍈🍍🍐🍖🍧🍆🍊🍞", 2, 4) returned "🍈🍍"
./22t2final_q6 "⇜⇿↴↛⇳⇙↜↧⇥↖⇸⇋⇈⇘↟↹⇝↩↥⇊⇇⇓⇟⇬⇩" 22 25
22t2final_q6("⇜⇿↴↛⇳⇙↜↧⇥↖⇸⇋⇈⇘↟↹⇝↩↥⇊⇇⇓⇟⇬⇩", 22, 25) returned "⇟⇬⇩"
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
You can assume that the given string is valid UTF-8.
22t2final_q6_main.c includes a main function to help you test your code.
You may not change the provided 22t2final_q6_main.c file.
You may not change 22t2final_q6 's prototype.
i.e. , 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.
No error checking is necessary or required.
🤖 Adapted: write a COMPLETE C program. Read a UTF-8 string from stdin (one trailing newline ignored).argv[1]=range_start (inclusive),argv[2]=range_end (exclusive), both code-point indices (clamped to the string length). Print the sliced substring + a newline.
Reference verified on the sandbox AND cross-checked against an independent code-point oracle.
Samples
hello world
hello
🍓🍇🍈🍍🍐
🍈🍍🍐