Reverse Pig Latin


General Statement:
Each word of a sentence has been translated into Pig Latin. Do a reverse translation and output the original sentence.

Input:
The first line of the data set for this problem is an integer that represents the number of sentences in the list. Each sentence is on a separate line.

Output:
Punctuation is to be output in its original location. All letters are upper case. The output is to be formatted exactly like that for the sample output given below.

Assumptions:
All letters are upper case.

Discussion:
To convert a word to Pig Latin, the first letter of the word was moved to the end (after the last letter of the word) and the letters “AY” were added.

For example, IGPAY was originally PIG.

Sample Input:
4
HEREWAY ASWAY ALDOWAY?
ELLOHAY, ARYMAY!
IMPLESAY IMONSAY ETMAY HETAY IEMANPAY.
AYHAY

Sample Output:
WHERE WAS WALDO?
HELLO, MARY!
SIMPLE SIMON MET THE PIEMAN.
HAY

Solution :

#include 
#include 
#include 
using namespace std;

void pig_to_normal (char str [])
{
    stack  s;
    char save;
    bool flag = true;

    for ( int i = strlen (str) - 1; i >= 0; i-- ) {
        if ( str [i] == 'Y' && str [i - 1] == 'A' && flag ) {
            save = str [i - 2];
            i -= 2;
            flag = false;
        }
        else
            s.push (str [i]);
    }

    s.push (save);

    while ( !s.empty () ) {
        printf ("%c", s.top ());
        s.pop ();
    }
}

int main ()
{
    int dataset;
    scanf ("%d", &dataset);
    getchar ();

    while ( dataset-- ) {
        char str [500];

        gets (str);

        char temp [100];
        int index = 0;

        for ( unsigned int i = 0; i <= strlen (str); i++ ) {
            if ( str [i] != ' ' && str [i] != 0 )
                temp [index++] = str [i];
            else {
                temp [index] = 0;
                pig_to_normal (temp);
                index = 0;
            }

            if ( str [i] == ' ' )
                printf (" ");
        }
        printf ("\n");
    }

    return 0;
}

Critical Cases

Sample Input : 
5
OMPLETECAYAYAY
XTEAYSLCAY
AYOLDZAY
.EOAY!!
HETAY!

Sample Output :
YOMPLETECAYA
CXTEAYSL
ZAYOLD
O.E!!
THE!
Advertisements

One thought on “Reverse Pig Latin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s