#P3583. Text Message Formatting
Text Message Formatting
Description
frkstyc has always wanted POJ to have a multiligual user interface. He has considered several schemes to implement the functionalities. Unfortunately, he has been occupied during the recent months, which prevents him from working on the development of POJ. Still, he would like to share some of his ideas in the hope that someone could finish the work for him.
One challenge in implementing a multilingual user interface is the manipulation of text messages. As you can easily identify, POJ has been developed with English as the primary language in its user interface. Hundreds of text messages that appear in web pages scatter in tens of source files as string resources. Isolating these string resources in the source files to faciliate the use of a unified programming interface for multilingualism support is itself a tedious and time-comsuming job and frequently triggers dismay in frkstyc. Besides, there are still some subtleties concerning differences between languages.
One particular such tricky aspect in which frkstyc is interested is word order. Consider the caption of the table in the page of Login Log. In English, it shall read “Last num login attempts by user”, where user appears after num. In Chinese, in contrast, it shall read “user的最后num次登录尝试”, where user appears before num. Such difference causes complexity. You may argue that the caption in English can be changed to “user’s last num login attempts” to avoid the trouble with Chinese. However, in the case of Spanish, it may not be that easy to reorder the words in “Los últimos num intentos de user por acceder” as in English.
frkstyc’s proposed solution is rather simplistic. He uses format strings with numbered placeholders. Take the aforementioned text messages for example. frkstyc creates the following three format strings
- Last {1} login attempts by {2}
- {2}的最后{1}次登录尝试
- Los últimos {1} intentos de {2} por acceder
where {1} and {2} are the numbered placeholders. By supplying a format string and the array of arguments [num, user] to a formatting function which replaces {1} with num and {2} with user, frkstyc always gets desired result regardless of the choice of language. Furthermore, the backslash is designated as the escape character. An unescaped backslash escapes the immediately following character, which shall be directly copied to the output and not further processed. A placeholder is recognized if none of its comprised characters is escaped and the number immediately surrounded by braces is a valid 1-based index into the array of arguments. A recognized placeholder shall be replaced in the output by array element indexed by the number in braces.
frkstyc recognizes writing the formatting function as the only fun part in implementing the envisioned multilingualism support. He delightfully invites you to share such joy by also writing the function.
Input
The input consists of a single test case. The test case contains at most 1001 lines, each being a string of at most 80 printable ASCII characters. The first string is the format string. The rest comprises the array of arguments.
Output
The output shall contain only the formatted string.
Last {1} login attempts by {2}
20
frkstyc
Last 20 login attempts by frkstyc
Source
POJ Founder Monthly Contest – 2008.04.13, frkstyc