You have a 128-byte long buffer that is initially filled with zeroes. Given a scanf format string, some offsets in the buffer, and some text, fill the buffer according to the format string and print the final contents of the buffer.
The format string consists of a sequence of directives which describe how to process the sequence of input characters. A directive is one of the following:
- A sequence of space characters. This directive matches any amount of space characters, including none, in the input.
- An ordinary character (i.e., one other than white space or '%'). This character must exactly match the next character of input.
- A conversion specification, which commences with a '%' (percent) character. A sequence of characters from the input is converted according to this specification, and the result is placed in the corresponding position in the buffer (the result of the ith conversion should be stored in the buffer starting at the ith offset).
- %u
- Matches an unsigned 4 byte long integer. It is stored in the buffer in little-endian order (least significant byte first).
For example, 1241251 (which is 12F0A316) is stored as A3 F0 12 00. - %x
- Matches 0x followed by an unsigned 4 byte long hexadecimal integer. It is stored in the buffer in little-endian order.
For example, 0xFEE1DEAD is stored as AD DE E1 FE. - %s
- Matches a sequence of non-white-space characters.
For example, hello is stored as 68 65 6C 6C 6F 00 - %c
- Matches a single character
For example, x is stored as 78
Input
On the first line, a format string and several integer offsets. The format string and offsets are separated by commas. There is no space before or after a comma.On the second line, the text you should scan.
Output
Exactly 256 uppercase hex digits (0-9A-F) representing the contents of the buffer. You may append a final newline.Notes
- The format string is at most 150 characters long.
- Each position in the buffer will be set at most once. In other words, the memory areas do not overlap.
- For the
%s
specifier it is guaranteed that the next character will NOT be a space character.
Sample
Input | Output |
---|---|
Alina are %u %s cu ID:%x,10,100,4 Alina are 3 mere cu ID:0xABCDEF | 00000000EFCDAB0000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006D657265000000000000000000000000000000000000000000000000 |
Things we have here: %c%c%c %s (less than %u units).,0,2,4,5,50 Things we have here: ^_^ word!with!bangs!!! (less than 40000 units). | 5E005F005E776F726421776974682162616E6773212121000000000000000000000000000000000000000000000000000000409C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
C solution (Marius Gavrilescu)
We can iterate over each character in the format string. If s[i] is %, we call scanf with s[i]s[i+1] and the next offset. Otherwise, we call scanf with s[i].1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 char s[205], f[3]; 6 unsigned char mem[128]; 7 8 int main(void){ 9 fgets(s, sizeof s, stdin); 10 strtok(s, ","); 11 12 for(int i = 0; s[i] ; i++) 13 if((*f = s[i]) == '%') 14 f[1] = s[++i], scanf(f, mem + atoi(strtok(NULL, ","))); 15 else 16 f[1] = 0, scanf(f); 17 18 for(int i = 0 ; i < 128 ; i++) 19 printf("%02X", mem[i]); 20 }