Write a program that reads lines of input from the user and converts each line i
ID: 3604446 • Letter: W
Question
Write a program that reads lines of input from the user and converts each line into "Pig Latin." Pig Latin is English with the initial consonant sound moved to the end of each word, followed by "ay". Words that begin with vowels simply have an "ay" appended. Your program should continuously prompt the user for sentences until they enter a blank line, at which point the program should terminate. Example: It-was-a-bright cold day in April and-the clocks were striking thirteen Result: It-ay as-way a-ay-ight-bray old-cay ay-day in-ay April-ay and-ay e- thay ocks-clay ere-way riking-stay irteen-thayExplanation / Answer
import java.util.Scanner;
public class PigLatin {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the Bhrase(EnglishwordJ:");
String sentence = input.nextLine();
sentence = sentence.replaceAll(" +", " ");
System.out.print("The phrase ");
System.out.println(""" + sentence + """ + " ");
Scanner word = new Scanner(sentence);
System.out.println("Would have the followingappearance in Pig Latin:");
System.out.print(""");
while (word.hasNext()) {
String pigLatin = word.next();
System.out.print(convertPigLatinPhrase(pigLatin));
}
}
public static String convertPigLatinWord(String inputWord)
{
int length = inputWord.length();
if (inputWord.charAt(length - 1) == '.'
|| inputWord.charAt(length - 1) == '!'
|| inputWord.charAt(length - 1) == '?')
{
char ch = inputWord.charAt(0);
String rest = inputWord.substring(1,length - 1);
return (rest + "-" + ch + "ay"+inputWord.charAt(length - 1) + """);
}else if((inputWord.charAt(0)=='t'||
inputWord.charAt(0)=='T')
&&inputWord.charAt(1)=='h')
{
return(convertTh(inputWord));
}else if( isVowel(inputWord.charAt(0)))
{
return (inputWord + "-ay");
}else {
char ch = inputWord.charAt(0);
String rest = inputWord.substring(1);
return (rest+"_"+ch+"ay");
}
}
public static String convertTh(String tWord)
{
char t tWord.charAt (0);
char h tWord.charAt (1);
String rest = tWord.substring(2);
return (rest + "-" + t + h + "ay");
}
public static String convertPigLatinPhrase(String englishPhrase)
{
String word = convertPigLatinWord(englishPhrase);
return (word + " ");
}
public static boolean isVowel(char c)
{
if (c == 'a' || c == 'A')
{
return (true);
} else if (c == 'e' || c == 'E') {
return (true);
} else if (c == 'i' || c == 'I') {
return (true);
} else if (c == '0' || c == '0') {
return (true);
} else if (c == 'u' || c == 'U') {
return (true);
} else {
return (false);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.