6-29 Esc Chars (20分)
Esc characters are represented as \x in C string, such as \n and \t.
Function prt_esc_chars() gets a string which may contains esc characters, and prints the string into the standard ouput with all esc characters been replaced by a \x format combination.
For example, using printf("%s"…, a string with a \n between the words will be printed as:
Hello
World
But the same string will be printed by prt_esc_chars() as:
Hello\nWorld
Your function should be able to recognize esc characters below:
\n
\r
\t
\b
And all other characters below 0x20 should be printed as:
\hh
where hh is the hexadecimal of the value, all letters in capital. For a value below 0x10, a leading 0 is needed to keep two positions.
And as a C string, 0x00 will not be part of the string but the terminator.
Be aware, printf() is forbidden in the function.
函数接口定义:
int prt_esc_char(const char *s);
s is the string to be printed. The function returns the number of characters printed. An esc character is counted as two or three according to the characters it uses.
裁判测试程序样例:
#include <stdio.h>
int prt_esc_char(const char *s);
int main()
{
char line = NULL;
size_t linecap = 0;
getline(&line, &linecap, stdin);
int len = prt_esc_char(line);
printf("%d\n", len);
}
/ 请在这里填写答案 */
输入样例:
hello world
There is a tab between hello and world.
输出样例:
hello\tworld\n14
6-29 Esc Chars (20分)
最新推荐文章于 2023-11-30 21:12:14 发布