Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PigLatin {
- public static void main(String args[]) {
- System.out.println("Elcomeway otay hetay Igpay Atinlay Eneratorgay!");
- Scanner input = new Scanner(System.in);
- while(true) {
- String text = input.next();
- if (text.equalsIgnoreCase("itquay")) {
- System.out.println("Oodbyegay.");
- System.exit(0);
- }
- else {
- System.out.print(toPigLatin(text) + " ");
- }
- }
- }
- private static String toPigLatin(String text) {
- if (!isThereAVowel(text))
- return "Invalidway.";
- String[] punctuation = getPunctuation(text);
- text = trimPunctuation(text, punctuation);
- if (beginsWithVowel(text))
- return punctuation[0] + text + "way" + punctuation[1];
- else {
- String firstConsonants = getFirstConsonants(text);
- text = text.substring(firstConsonants.length());
- if (isCapitalized(firstConsonants))
- text = capitalize(text);
- return punctuation[0] + text + firstConsonants.toLowerCase() + "ay" + punctuation[1];
- }
- }
- private static boolean isThereAVowel(String text) {
- for (int i = 0; i < text.length(); i++) {
- String letter = text.charAt(i) + "";
- if (isAVowel(text.charAt(i) + ""))
- return true;
- }
- return false;
- }
- private static String[] getPunctuation(String text) {
- String firstPunctuation = "";
- int i = 0;
- while (isPunctuation(text.charAt(i) + "")) {
- firstPunctuation += (text.charAt(i) + "");
- i++;
- }
- String lastPunctuation = "";
- i = text.length() - 1;
- while (isPunctuation(text.charAt(i) + "")) {
- lastPunctuation = (text.charAt(i) + "") + lastPunctuation;
- i--;
- }
- String[] toReturn = {firstPunctuation, lastPunctuation};
- return toReturn;
- }
- private static String trimPunctuation(String text, String[] punctuation) {
- text = text.substring(punctuation[0].length(), text.length() - punctuation[1].length());
- return text;
- }
- private static boolean beginsWithVowel(String text) {
- if (isAVowel(text.charAt(0) + ""))
- return true;
- return false;
- }
- private static String getFirstConsonants(String text) {
- String consonants = "";
- int i = 0;
- while (!isAVowel(text.charAt(i) + "")) {
- consonants += (text.charAt(i) + "");
- i++;
- }
- if ((text.charAt(i - 1) + "").equalsIgnoreCase("q") && (text.charAt(i) + "").equalsIgnoreCase("u"))
- consonants += "u";
- return consonants;
- }
- private static boolean isCapitalized(String text) {
- if ((text.charAt(0)) == Character.toUpperCase(text.charAt(0)))
- return true;
- return false;
- }
- private static String capitalize(String text) {
- char firstChar = text.charAt(0);
- text = text.substring(1);
- return Character.toUpperCase(firstChar) + text;
- }
- private static boolean isAVowel(String letter) {
- String[] vowels = {"a", "e", "i", "o", "u"};
- for (int i = 0; i < vowels.length; i++)
- if (letter.equalsIgnoreCase(vowels[i]))
- return true;
- return false;
- }
- private static boolean isPunctuation(String character) {
- String[] punctuation = {"'", "[", "]", "(", ")", "{", "}", "<", ">", ":", ",", "-", "!", ".", "?", "\"", ";", "/", " ", "&", "*", "@", "\\", "^", "ยก", "ยฟ", "#", "%", "+", "~", "_", "|", "`", "$"};
- for (int i = 0; i < punctuation.length; i++)
- if (character.equalsIgnoreCase(punctuation[i]))
- return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement