-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPigLatin.java
More file actions
46 lines (43 loc) · 1.25 KB
/
Copy pathPigLatin.java
File metadata and controls
46 lines (43 loc) · 1.25 KB
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
import java.util.Scanner;
public class PigLatin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String[] line = sc.nextLine().split(" ");
for (int i = 0; i < line.length; i++) {
int loc = vowelLocation(line[i]);
if(loc == 0){
line[i] += "yay";
}
else{
String prefix = line[i].substring(0, loc);
line[i] = line[i].substring(loc) + prefix + "ay";
}
}
for (String string : line) {
System.out.printf("%s ",string);
}
}
sc.close();
}
private static int vowelLocation(String string) {
//returns the location of the first vowel
for (int i = 0; i < string.length(); i++) {
if(isVowel(string.charAt(i))) return i;
}
return -1;
}
private static boolean isVowel(char c) {
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}
}