
% Homework 3 Solutions
% pigLatin.m
% CS 1109, Summer 2011
% July 18

% This script prompts the user to enter a phrase and translates each word 
% into Pig Latin.
% 
% Assumptions:
%
% 1) The phrase only has lower-case letters (no upper-case letters,
% numbers, symbols or punctuation marks).
%
% 2) There are no words with no vowels in the user-input (for example,
% words like rhythm, tryst or gypsy).
%

clc
clear

phrase = input('Enter a phrase: ', 's');

% Extract the first word from the input phrase
[firstWord rest] = strtok(phrase);

% While there are still words to be processed, translate each word into Pig
% Latin
while (~isempty(firstWord))
    
    % Translating into Pig Latin:
    % First, find the position where the first vowel appears
    i = findFirstVowel(firstWord);
    
    % If the word begins with a vowel, simply append 'way' to the end of
    % it. Otherwise, transpose the first consonant cluster to the end and
    % append 'ay' to the end of the word.
    if (i == 1)
        fprintf('%sway ', firstWord);
    else
        fprintf('%s%say ', firstWord(i:end), firstWord(1:i-1));
    end
    
    % Get the next word
    [firstWord rest] = strtok(rest);
end

fprintf('\n');

% Common mistakes / other observations:
%
% 1) The most common mistake was code that failed to handle the case where
% the user input consisted of just a single word (i.e. people testing if
% `rest' was empty, rather than if `firstWord' was empty). Note that
% testing if `rest' is empty as the stopping condition for the while-loop
% will not produce any output if the input consists of a single word.
%
% 2) Many also failed to correctly handle the case where a word begins with
% a vowel.
%
% 3) Other code standard violations seem to be consistently repeated --
% missing headers, clc/clear statements etc.
%
