ZeroJudge C007: TeX Quotes

UVa 00272 – TeX Quotes

TeX is a typesetting software developed by Donald Knuth. This software can take raw document files and, after adding elements like fonts, turn them into a beautifully formatted document. A beautiful document needs to use directional double quotes “ and ” to quote someone’s words, rather than the regular double quotes ” found on most keyboards. Although keyboards usually do not have directional double quote keys, they do have the left single quote (sometimes called Backquote) and the right single quote ' (sometimes called Apostrophe or Quote). You can find these on your keyboard, but be careful not to confuse with \ (Backslash key). In TeX, users can input two left single quotes `` to create a left double quote “, or two right single quotes '' to create a right double quote ”. However, people usually get used to typing regular double quotes ” when quoting someone.

If the content of the original document file is:

“To be or not to be,” quoth the bard, “that is the question.”

Then the document produced by TeX would not be what we want:

To be or not to be,” quoth the bard, that is the question.”

In order to get the desired document, we need to transform the original document into this format:

To be or not to be,” quoth the bard, that is the question.”

你現在必須要寫一個程式,將普通的雙引號(”),轉成有方向性的雙引號,而其它文字則不變。而在把普通的雙引號換掉的時候,要特別注意,當要開始引述一句話時要用 ,而結束引述時要用 ” 。不用擔心會有多層巢狀引號的情形,也就是第一個引號一定是用 來代替,再來用 ”,然後用 “ ,接著用 ” ,依此類推。

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
EOF inputs: each data entry contains a string with space characters.
The output text must be identical to the input, except for the following:
The first " in each pair of double quotes must be replaced with two ` characters (i.e., “).
The second " in each pair of double quotes must be replaced with two ‘ characters (i.e., ”).
Output the updated string.
“To be or not to be,” quoth the Bard, “that
is the question”.
The programming contestant replied: “I must disagree.
To C' or not toC’, that is The Question!”
“To be or not to be,” quoth the Bard, that
is the question”.
The programming contestant replied: “I must disagree. To `C’ or not to `C’, that is The Question!”
ZeroJudge C007 範例測資

Thought Process

Use getline to read each string and iterate through each character using a for loop. If the current character is ", you can declare a boolean variable initialized to true. If the boolean value is true, output “ and set the boolean value to false. If the boolean value is false, output ” and set the boolean value to true.

The boolean value needs to be declared outside the EOF.

Sample Code-ZeroJudge C007: TeX Quotes

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    string str;
    bool start = true;
    while (getline(cin, str))
    {
        for (int i = 0; i<str.length(); i++)
        {
            if (str[i] == '"')
            {
                if (start)
                {
                    cout << "``";
                    start = false;
                }
                else
                {
                    cout << "''";
                    start = true;
                }
            }
            else cout << str[i];
        }
        cout << "\n";
    }
}

//ZeroJudge C007
//Dr. SeanXD

Comments