I've created a program that translates any word into the form of igpay atinlay or pig Latin!
I got the project idea from here and you can read the rules here.
My program outputs the same translation as in the rules section in the link above, except for the word "always". It isn't really a big deal as the program translates almost any word correctly.
package pigLatin;
import java.util.Scanner;
public class PigLatin
{
String str;
Scanner scan = new Scanner(System.in);
char firstVow;
char secondVow;
int i;
public void init()
{
str = scan.next();
i = 0; //beginning of a string
if (str.length() <= 3 && (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u'))
{
System.out.println(str + "ay");
scan.close();
}
else if (str.length() > 3 && (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u')) {
firstVow = str.charAt(i);
i = 1;
while (str.charAt(i) != 'a' || str.charAt(i) != 'e' || str.charAt(i) != 'i' || str.charAt(i) != 'o' || str.charAt(i) != 'u')
{
i++;
if (i == str.length() - 1 && (str.charAt(i) != 'a' || str.charAt(i) != 'e' || str.charAt(i) != 'i' || str.charAt(i) != 'o' || str.charAt(i) != 'u')) {
System.out.println(str + "ay");
break;
} else if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u')
{
secondVow = str.charAt(i);
//issues arise if the first vowel is equal to the second one
if (firstVow != secondVow) {
System.out.print(str.substring(str.indexOf(secondVow), str.length()));
System.out.print(str.substring(str.indexOf(firstVow), str.indexOf(secondVow)));
System.out.println("ay");
break;
} else if (firstVow == secondVow) {
System.out.print(str.substring(2));
System.out.print(firstVow);
System.out.print(str.charAt(1));
System.out.print("ay");
break;
}
scan.close();
}
}
//checks for words that begin with consonants
} else {
i = 0;
while (str.charAt(i) != 'a' || str.charAt(i) != 'e' || str.charAt(i) != 'i' || str.charAt(i) != 'o' || str.charAt(i) != 'u')
{
i++;
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u')
{
firstVow = str.charAt(i);
System.out.print(str.substring(str.indexOf(firstVow), str.length()) + str.substring(0, str.indexOf(firstVow)) + "ay");
break;
}
}
}
scan.close();
}
public static void main(String[] args) {
new PigLatin().init();
}
}