-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPigLatin.java
66 lines (58 loc) · 1.72 KB
/
PigLatin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.learnjava.beginner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class PigLatin {
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length == 0) {
System.out.println("Provide a word or a sentence an press Enter");
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
String piglatinized = PigLatin.wordToLatin(text);
System.out.println("Your Pig Latin is : " + piglatinized);
}else {
System.out.println("Your piglatinized words are: ");
for(String word : args) {
String piglatin = PigLatin.wordToLatin(word);
System.out.println("Word: " + word + " ---- " + piglatin);
}
}
}
private static String wordToLatin(String word) {
String latin = "";
boolean isvowel = PigLatin.firstCharisVowel(word.charAt(0));
if(isvowel) {
latin += word + "ay";
}
else {
String consonantcluster = PigLatin.getConsonantCluster(word);
latin += word.substring(word.indexOf(consonantcluster) + consonantcluster.length()) + consonantcluster + "ay";
}
return latin;
}
private static String getConsonantCluster(String word) {
String consonantcluster = "";
for(int i = 0; i < word.length(); i++) {
char current_char = word.charAt(i);
if(PigLatin.firstCharisVowel(current_char)) {
break;
}else {
consonantcluster += current_char;
}
}
return consonantcluster;
}
private static boolean firstCharisVowel(char firstcharacter) {
boolean isvowel = false;
String vowels = "aeiou";
for(int i = 0; i < vowels.length(); i++) {
char character = vowels.charAt(i);
if(character == firstcharacter) {
isvowel = true;
break;
}
}
return isvowel;
}
}