//Homework 2

//Problem 2

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

#include <cstring>

void print_lat_phrase(char *);
void printLatinWord(char *, int);
void move_to_end(char *, int);
void swap(char &, char &);

int main()
{
	char *eng_phrase;
	
	cout << "Input an English phrase (without punctuation) to be translated into Pig Latin." << endl;
	cin.getline( eng_phrase, 100, '\n');
	
	print_lat_phrase(eng_phrase);
		
	return(0);

}

void print_lat_phrase(char *eng_phrase)
{	
	char *token_ptr;
	char *space = " ";
	int n;
	
	token_ptr = strtok(eng_phrase, space);
	
	do {
		n = strlen(token_ptr);
		printLatinWord(token_ptr, n);		
		token_ptr = strtok(NULL, space);

	} while (token_ptr != NULL);
	
	cout << endl;
}

void printLatinWord(char *eng_word_ptr, int n)
{
	char *temp;
	temp = new char[n];
	const char *suffix = "ay";
	
	for(int i=0; i<n; i++) {
		temp[i] = eng_word_ptr[i];
	}
	
	move_to_end(temp, 0);
	strcat(temp, suffix);
	
	cout << temp << " ";
}

void move_to_end(char *word, int k)
{	
	int n = strlen(word);
	
	for(int i=k; i<(n-1); i++) {
		swap(word[i], word[i+1]);
	}
}

void swap(char &a, char &b)
{
	char temp = a;
	a = b;
	b = temp;
}
