Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class piglatin {
  7.  
  8. private static boolean startWithVowel(String word) {
  9. return "aeiouy".indexOf(word.charAt(0)) >= 0;
  10. }
  11.  
  12. private static int indexOfFirstVowel(String word) {
  13. int index = 0;
  14.  
  15. for (index = 0; index < word.length(); index++) {
  16. if (VOWELS.contains(String.valueOf(word.charAt(index)))) {
  17. return index;
  18. }
  19. }
  20. return -1;
  21. }
  22.  
  23.  
  24. final static String VOWELS = "aeiouy";
  25.  
  26. public static void main(String[] args) throws IOException {
  27.  
  28. String word = "";
  29. String[] words;
  30. String yay = "yay";
  31. String ay = "ay";
  32. Scanner scanner = new Scanner(System.in);
  33.  
  34. List<String> sentence;
  35. while (!"".contentEquals(word = scanner.nextLine())) {
  36. words = word.split("\\s");
  37. sentence = new LinkedList<>();
  38. for (String value : words) {
  39. StringBuilder stringBuilder = new StringBuilder(value);
  40. if (startWithVowel(stringBuilder.toString())) {
  41. stringBuilder.append(yay);
  42. stringBuilder.trimToSize();
  43. sentence.add(stringBuilder.toString());
  44. } else {
  45. String tmp = stringBuilder.substring(0, indexOfFirstVowel(value));
  46. stringBuilder.delete(0, tmp.length());
  47. stringBuilder.append(tmp);
  48. stringBuilder.append(ay);
  49. stringBuilder.trimToSize();
  50. sentence.add(stringBuilder.toString());
  51. }
  52. }
  53. sentence.stream().map(s -> s + " ").forEach(System.out::print);
  54. System.out.println();
  55. }
  56. scanner.close();
  57.  
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement