Advertisement
Guest User

Pig Latin?

a guest
Jan 27th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. public class PigLatin
  2. {
  3.  
  4. public static void main(String[] args)
  5. {
  6. String test = "rhythm";
  7.  
  8. parse(test);
  9. }
  10.  
  11. private static void parse(String str)
  12. {
  13. String toMove = "";
  14. boolean isWordConsonant = false;
  15. boolean isGroundZero = true;
  16. boolean isYCase = false;
  17.  
  18.  
  19. for(int i = 0; i < str.length(); i++)
  20. {
  21. //This boolean is true if this is the first letter of the word
  22. if(isGroundZero)
  23. {
  24. //Check if word is consonant
  25. if(!isVowel(str.charAt(0)))
  26. isWordConsonant = true;
  27. else
  28. if(str.charAt(0) == 'y') // Y case
  29. isWordConsonant = true;
  30. isGroundZero = false;
  31. }
  32.  
  33. if(isWordConsonant)
  34. {
  35. //Skip Y case
  36. if(i < 1 && str.charAt(0) == 'y')
  37. {
  38. toMove += str.charAt(i);
  39. }
  40. else
  41. {
  42. //Check to see if this letter is a consonant
  43. if(!isVowel(str.charAt(i)))
  44. toMove += str.charAt(i);
  45. else
  46. {
  47. if(str.charAt(i) == 'y')
  48. isYCase = true;
  49. break;
  50. }
  51. }
  52. }
  53. else
  54. if(isVowel(str.charAt(i)))
  55. break;
  56. }
  57.  
  58. //Move letters and make new string
  59. String newWord;
  60.  
  61. if(isWordConsonant && !isYCase)
  62. newWord = str.substring(toMove.length()) + toMove + "ay";
  63. else if(isWordConsonant && isYCase)
  64. newWord = str.substring(toMove.length()) + toMove + "way";
  65. else
  66. newWord = str += "way";
  67.  
  68.  
  69. System.out.println("The new world is: " + newWord);
  70. }
  71.  
  72. public static boolean isVowel(char c)
  73. {
  74. switch(c)
  75. {
  76. case 'a':
  77. case 'e':
  78. case 'i':
  79. case 'o':
  80. case 'u':
  81. case 'y':
  82. return true;
  83. }
  84. return false;
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement