Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PigLatin
- {
- public static void main(String[] args)
- {
- String test = "rhythm";
- parse(test);
- }
- private static void parse(String str)
- {
- String toMove = "";
- boolean isWordConsonant = false;
- boolean isGroundZero = true;
- boolean isYCase = false;
- for(int i = 0; i < str.length(); i++)
- {
- //This boolean is true if this is the first letter of the word
- if(isGroundZero)
- {
- //Check if word is consonant
- if(!isVowel(str.charAt(0)))
- isWordConsonant = true;
- else
- if(str.charAt(0) == 'y') // Y case
- isWordConsonant = true;
- isGroundZero = false;
- }
- if(isWordConsonant)
- {
- //Skip Y case
- if(i < 1 && str.charAt(0) == 'y')
- {
- toMove += str.charAt(i);
- }
- else
- {
- //Check to see if this letter is a consonant
- if(!isVowel(str.charAt(i)))
- toMove += str.charAt(i);
- else
- {
- if(str.charAt(i) == 'y')
- isYCase = true;
- break;
- }
- }
- }
- else
- if(isVowel(str.charAt(i)))
- break;
- }
- //Move letters and make new string
- String newWord;
- if(isWordConsonant && !isYCase)
- newWord = str.substring(toMove.length()) + toMove + "ay";
- else if(isWordConsonant && isYCase)
- newWord = str.substring(toMove.length()) + toMove + "way";
- else
- newWord = str += "way";
- System.out.println("The new world is: " + newWord);
- }
- public static boolean isVowel(char c)
- {
- switch(c)
- {
- case 'a':
- case 'e':
- case 'i':
- case 'o':
- case 'u':
- case 'y':
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement