Friday, March 6, 2015

Pig Latin converter

So I made this pig latin translator in class today. It doesn't handle capitalization or punctuation, but it can handle multiple words at once, separated by spaces.

import javax.swing.JOptionPane;

public class home {
static String[] vowels = {"a", "e", "i", "o", "u", "y"};
public static void main(String[] args) {
String input;
String out;
boolean x = true;
while (x) {
input = JOptionPane.showInputDialog("Words to change?");
if (input.equals("stop")){
x=false;
break;
}
out = "";
for (String ret: input.split(" ")){
int vpos = find(ret);
System.out.println(vpos);
if (vpos > 0) {
out = out + ret.substring(vpos) + ret.substring(0, vpos) + "ay ";
} else {
out = out + ret + "way ";
}
}
JOptionPane.showMessageDialog(null, out);
}
}
public static int find(String inp) {
int pos = -1;
for (String vwl: vowels) {
int vpos = inp.indexOf(vwl);
if ((vpos >= 0) && ((vpos < pos)||(pos==-1)) && (vpos != inp.indexOf("qu")+1)) {
pos=inp.indexOf(vwl);
}
}
return pos;
}
}


No comments:

Post a Comment