Table of Contents

Pig-Latin

Problem

  • A simplified pig latin translator translates as follows:
    • If the word starts with a vowel
      • add “way” to the end of the word
    • Otherwise
      • move all letters before the first vowel to the end of the word
      • add “ay” to the end of the word
  • Write a program to translate user input into pig latin
  • Use “opstay” as a sentinel
  • Print the translation only after the sentinel is given
  • Follow the problem solving steps to design a solution
  • Be sure to comment your functions

Solution

/*
Test case 1:
Input: "hello there opstay" (words starting with consonants)
Expected Output: "ellohay erethay"
Actual:
 
Test case 2:
Input: "im excited to meet you opstay"(words starting with vowels)
Expected Output: "imway excitedway otay eetmay ouyay"
Actual:
 
Test case 3:
Input: "opstay" (no sentence)
Expected Output: ""
Actual: 
*/
#include <iostream>
#include <string>
 
using namespace std;
 
/*
	This function finds and returns the position of the first lower-case vowel in the input word. 
	Note that as is, if the word does not have vowel, it will return one past the end of the word.
	@param word word in which to find the position of the first vowel
	@return the 0-based position of the first vowel in the input word
*/
int find_pos_of_first_vowel(string word)
{
	int pos = -1;
 
	// for each position in the word
	for (pos = 0; pos < word.length(); pos++)
	{
		// check if the letter at that position is a vowel
		string char_at_pos = word.substr(pos, 1);
		if (char_at_pos == "a" || char_at_pos == "e"
			|| char_at_pos == "i" || char_at_pos == "o"
			|| char_at_pos == "u")
		{
			// as soon as it is, we've found the first value
			return pos; // this returns immediately out of the function
		}
	}
 
	return pos;
}
 
/*
	Function to translate one word into its pig latin equivalent
	@param word word to translate
	@return translated word
*/
string translate_into_pig_latin(string word)
{
	string translated_word = word;
 
	int pos_of_first_vowel = find_pos_of_first_vowel(word);
 
	// You can use cout statements in your functions to check that it's working correctly
	// cout << pos_of_first_vowel << endl;
 
	// If the word starts with a vowel
	if (pos_of_first_vowel == 0)
	{
 
		//add “way” to the end of the word
		translated_word = word + "way";
	}
	// Otherwise
	else
	{
		// move all letters before the first vowel to the end of the word
		translated_word = word.substr(pos_of_first_vowel) + word.substr(0, pos_of_first_vowel) + "ay";
		// add “ay” to the end of the word
 
	}
	return translated_word;
}
 
int main()
{
	// Prompt the user for input
	cout << "Gimme a sentence all lowercase to translate (\"opstay\" to end): ";
	string word;
	string translated_sentence = "";
	// read the input
	cin >> word;
 
	// while the input isn't "opstay"
	while (word != "opstay")
	{
		//translate the input into pig latin and store it to print out later
		translated_sentence = translated_sentence + " " + translate_into_pig_latin(word);
 
		// get the next word
		cin >> word;
	}
 
	// Print out
	cout << "Your sentence in pig latin is: " << translated_sentence << endl;
 
	system("pause");
	return 0;
}
cs-142/pig-latin.txt · Last modified: 2015/05/27 05:15 by cs142ta
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0