Please could someone help me to solve a small issue. I was given an assignment to convert an english word into opish. Opish is a language that adds the characters "op" after every consonant and if necessary before the next vowel. My code works for most words, however, if two consonants are next to each other, it fails. As example the word chat should be chopatop, but the code returns cophopatop as it is not checking to see if the next character is a vowel. Here is the code:
public class Translate {
/**
* Is char a vowel?
*/
public static boolean isConsonant(char c) {
boolean consonant;
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' ||
c == 'O' || c == 'U'){
consonant = false; System.out.println("No");}
else{
consonant = true; System.out.println("Yes");}
return consonant; }
/**
* Is char a vowel?
*/
public static boolean isVowel(char c) {
boolean vowel;
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' ||
c == 'O' || c == 'U'){
vowel = true; }
else{
vowel = false; }
return vowel; }
/**
* converts given word to Pig Latin
*/
public static String toPigLatin(String word) {
int i;
boolean c;
for (i = 0; i<word.length(); i++){
c = isConsonant(word.charAt(i));
if (c == false){
String d = word.substring(i,word.length());
System.out.println(d);
String a = word.substring(0,i);
System.out.println(a);
return (d + a + "ay");
}
}
return word;
}
/**
* converts given word to Opish
*/
public static String toOpish(String word) {
int i;
boolean c;
boolean v;
for (i = 0; i<word.length(); i++){
c = isConsonant(word.charAt(i));
v = isVowel(word.charAt(i));
if (c == true) {
String front = word.substring(0,i+1);
String back = word.substring(i+1,word.length());
word = front + "op" + back ;
i=i+3;
System.out.println("word " + word);
}
}
return word;}
} // end class