题目要求:
在TeX中,左双引号是“``”,右双引号是“''”。输入一篇包含双引号的文章,你的任务是
把它转换成TeX的格式。
样例输入:
"To be or not to be," quoth the Bard, "that
is the question".
样例输出:
``To be or not to be,'' quoth the Bard, ``that
is the question''.
分析:
使用一个变量q来判断一个双引号是左双引号还是右双引号。
之前学习了使用“scanf("%s")”输入字符串,但却不能在本题中使用它,因为它碰到空格
或者TAB就会停下来。虽然下次调用时会输入下一个字符串,可是不知道两次输入的字符串
中间有多少个空格、TAB甚至换行符。所以用 while((c = getchar()) != EOF) 进行输入。
代码:
#include<stdio.h>
int main()
{
int c, q = 1;
while((c = getchar()) != EOF)
{
if(c == '"')
{
printf("%s", q ? "``" : "''");
q = !q;
}
else printf("%c", c);
}
return 0;
}