online advertising

Monday, September 8, 2014

UVa Problem 272 - TEX Quotes

Problem:

Please find the problem here.

Solution:

This is really just a simple state machine. Initially it is in notInQuote state, when we see a double quote, translate that into two back tick characters and go to the InQuote state. When we are in the InQuote state, and when we encounter a double quote, translate it into two single quotes, and we are done!

The only thing we need to be careful is the I/O routines.

Code:

#include "stdafx.h"

// http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=208

#include "UVa272.h"

#include <iostream>

using namespace std;

int UVa272()
{
    bool inQuotes = false;
    while (!cin.eof())
    {
        char c = cin.get();
        if (c != -1)
        {
            if (c == '"')
            {
                if (inQuotes)
                {
                    cout << "''";
                    inQuotes = false;
                }
                else
                {
                    cout << "``";
                    inQuotes = true;
                }
            }
            else
            {
                cout << c;
            }
        }
    }

    return 0;
}

No comments :

Post a Comment