UVa272 - TEX Quotes 题解

题目链接TEX Quotes

题目大意

在TEX中,左双引号是“``”,右双引号是“''”。输入一篇包含双引号的文章,你的任务是把它转换成TEX的格式。

样例输入

"To be or not to be," quoth the Bard, "that is the question".

样例输出

``To be or not to be,'' quothe the Bard, ``that is the question''.

本题的程序还是分为3个部分。读入输入,处理,输出(废话)。
读入输入与输出没话说。关键就在处理时候,如何判断是左引号还是右引号?
答案就是设置一个标识变量。

#include <stdio.h>

int main(){
    char c, flag=0;
    //当输入结束时,getchar()返回值为EOF
    while((c = getchar()) != EOF){
        if(c == '\"'){
            //左引号
            if(flag == 0){
                printf("``");
                flag=1;
            //右引号
            }else{
                printf("''");
                flag=0;
            }
        }else{
            printf("%c", c);
        }
    }
    return 0;
}
posted @ 2020-10-27 07:06  1v7w  阅读(68)  评论(0编辑  收藏  举报