Patrick B. answered 07/16/19
Math and computer tutor/teacher
The user can input more text AFTER the period. The input will not terminate if the ENTER key is not pressed. Therefore, you can/must process more than one sentence at a time.
So if the word begins with a vowel, you leave the word as is without moving anything, and just append yay at the end. Otherwise you move the leading consonant to the end of the word, and append ay at the end.
But.... what if the word begins with a double consonant... You must move BOTH consonants to the end, before appending the ay. THe same rule applies to words beginning with qu and ?h where ? is consonant.
For example:
The quick brown fox jumped over the lazy dogs. That is all folks <eof>
Ethay ickquay ownbray oxfay umpedjay overay ethay azylay ogsday. atsThay isay allay olksfay.
Here's the source code:
=================================
import java.io.*;
class PigLatin
{
private Console console;
private String [] sentences;
PigLatin()
{
console = System.console();
}
private boolean IsLeadingVowel( String charStr)
{
boolean boolReturn = false;
System.out.println("IsLeadingVowel: "+ charStr);
if (charStr.equals("A") || charStr.equals("E") || charStr.equals("I") || charStr.equals("O") || charStr.equals("U"))
{
boolReturn = true;
}
return(boolReturn);
}
private boolean IsLeadingDoubleConsonant( String charStr)
{
boolean boolReturn = false;
System.out.println(" IsLeadingDoubleConsonant:"+charStr);
if ((charStr.equals("TH")) || (charStr.equals("QU")))
{
boolReturn = true;
}
if (
!(
IsLeadingVowel(charStr.substring(0,1)) ||
IsLeadingVowel(charStr.substring(1,2))
)
)
{
boolReturn = true;
}
return(boolReturn);
}
public String Translate( String inputSentence)
{
String [] tokens = inputSentence.split(" ");
String outbuff = " ";
System.out.println(inputSentence);
for (int iLoop=0; iLoop<tokens.length; iLoop++)
{
if (tokens[iLoop].length()==0) { continue; }
String inbuff = (tokens[iLoop].trim()).toUpperCase();
System.out.println(" Translating " + inbuff );
//translation occurs here
String slice1 = inbuff.substring(0,1);
String slice2 = inbuff.substring(0,2);
System.out.println(" SLICE#1 = " + slice1 + " AND SLICE #2 = " + slice2);
if (IsLeadingDoubleConsonant(slice2))
{
outbuff = outbuff + inbuff.substring(2) + slice2 + "ay";
}
else
{
if (IsLeadingVowel(slice1))
{
outbuff = outbuff + inbuff + "ay";
}
else
{
outbuff = outbuff + inbuff.substring(1) + slice1 + "ay";
}
} //translation
outbuff = outbuff + " ";
} //for loop
return(outbuff);
}
public void Go()
{
String inbuff;
System.out.print(" Please input sentences with a period after each sentence. \n"+
" Press ENTER when finished :>");
inbuff = console.readLine();
// System.out.println(inbuff);
sentences = inbuff.split("\\.|\\?|\\!"); //splits the paragraph over ending punctuation
System.out.println(sentences.length);
for (int iLoop=0; iLoop<sentences.length; iLoop++)
{
String outbuff = Translate(sentences[iLoop].trim());
System.out.println(outbuff);
}
}
public static void main(String args[])
{
PigLatin myPigLatin = new PigLatin();
myPigLatin.Go();
} //main
} //class