Advertisement
wtmhahagd

Pig Latin

Aug 26th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PigLatin {
  4.  
  5. public static void main(String args[]) {
  6. System.out.println("Elcomeway otay hetay Igpay Atinlay Eneratorgay!");
  7.  
  8. Scanner input = new Scanner(System.in);
  9.  
  10. while(true) {
  11. String text = input.next();
  12. if (text.equalsIgnoreCase("itquay")) {
  13. System.out.println("Oodbyegay.");
  14. System.exit(0);
  15. }
  16. else {
  17. System.out.print(toPigLatin(text) + " ");
  18. }
  19. }
  20. }
  21.  
  22. private static String toPigLatin(String text) {
  23. if (!isThereAVowel(text))
  24. return "Invalidway.";
  25.  
  26. String[] punctuation = getPunctuation(text);
  27. text = trimPunctuation(text, punctuation);
  28.  
  29. if (beginsWithVowel(text))
  30. return punctuation[0] + text + "way" + punctuation[1];
  31. else {
  32. String firstConsonants = getFirstConsonants(text);
  33. text = text.substring(firstConsonants.length());
  34.  
  35. if (isCapitalized(firstConsonants))
  36. text = capitalize(text);
  37.  
  38. return punctuation[0] + text + firstConsonants.toLowerCase() + "ay" + punctuation[1];
  39. }
  40. }
  41.  
  42. private static boolean isThereAVowel(String text) {
  43. for (int i = 0; i < text.length(); i++) {
  44. String letter = text.charAt(i) + "";
  45. if (isAVowel(text.charAt(i) + ""))
  46. return true;
  47. }
  48. return false;
  49. }
  50.  
  51. private static String[] getPunctuation(String text) {
  52. String firstPunctuation = "";
  53. int i = 0;
  54. while (isPunctuation(text.charAt(i) + "")) {
  55. firstPunctuation += (text.charAt(i) + "");
  56. i++;
  57. }
  58.  
  59. String lastPunctuation = "";
  60. i = text.length() - 1;
  61. while (isPunctuation(text.charAt(i) + "")) {
  62. lastPunctuation = (text.charAt(i) + "") + lastPunctuation;
  63. i--;
  64. }
  65.  
  66. String[] toReturn = {firstPunctuation, lastPunctuation};
  67. return toReturn;
  68. }
  69.  
  70. private static String trimPunctuation(String text, String[] punctuation) {
  71. text = text.substring(punctuation[0].length(), text.length() - punctuation[1].length());
  72. return text;
  73. }
  74.  
  75. private static boolean beginsWithVowel(String text) {
  76. if (isAVowel(text.charAt(0) + ""))
  77. return true;
  78. return false;
  79. }
  80.  
  81. private static String getFirstConsonants(String text) {
  82. String consonants = "";
  83. int i = 0;
  84. while (!isAVowel(text.charAt(i) + "")) {
  85. consonants += (text.charAt(i) + "");
  86. i++;
  87. }
  88. if ((text.charAt(i - 1) + "").equalsIgnoreCase("q") && (text.charAt(i) + "").equalsIgnoreCase("u"))
  89. consonants += "u";
  90. return consonants;
  91. }
  92.  
  93. private static boolean isCapitalized(String text) {
  94. if ((text.charAt(0)) == Character.toUpperCase(text.charAt(0)))
  95. return true;
  96. return false;
  97. }
  98.  
  99. private static String capitalize(String text) {
  100. char firstChar = text.charAt(0);
  101. text = text.substring(1);
  102. return Character.toUpperCase(firstChar) + text;
  103. }
  104.  
  105. private static boolean isAVowel(String letter) {
  106. String[] vowels = {"a", "e", "i", "o", "u"};
  107. for (int i = 0; i < vowels.length; i++)
  108. if (letter.equalsIgnoreCase(vowels[i]))
  109. return true;
  110. return false;
  111. }
  112.  
  113. private static boolean isPunctuation(String character) {
  114. String[] punctuation = {"'", "[", "]", "(", ")", "{", "}", "<", ">", ":", ",", "-", "!", ".", "?", "\"", ";", "/", " ", "&", "*", "@", "\\", "^", "ยก", "ยฟ", "#", "%", "+", "~", "_", "|", "`", "$"};
  115. for (int i = 0; i < punctuation.length; i++)
  116. if (character.equalsIgnoreCase(punctuation[i]))
  117. return true;
  118. return false;
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement