user3020346
user3020346

Reputation: 41

adding string to certain char

doing a lab for class ( cannot use ARRAY )** its a langue converter and i have to put 'ub' after every vowel i was wondering how i could do this WITHOUT AN ARRAY so far i have but it just adds "ub" after the second letter in a string

private static String toUbbi(String word ) {            
    String set = " ";
    if (Vowel (word)){
        set=  word.substring(0)+ "ub"+word.substring(1) ;
        set = word.substring(0,1)+ "ub"+word.substring(1);
    }
    return set;         
}     

private static boolean Vowel(String word ) {             
String[] vowels ={ "a", "e", "i", "o", "u", "ue"} ; 
    //char x = word.charAt(0);
    return (vowels.length !=-1);     
} 

Upvotes: 0

Views: 80

Answers (2)

Christian Tapia
Christian Tapia

Reputation: 34146

You can try this:

public static void main(String[] args)
{
    String str = "christian";
    String new_str = "";

    for (int i = 0; i < str.length(); i++) {
        new_str += str.charAt(i);
        if (isVowel(str.charAt(i))) // If is a vowel
            new_str += "ub";
    }
    System.out.println(new_str);
}

private static boolean isVowel(char c)
{
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
        return true;
    return false;
}

Upvotes: 0

Ashish
Ashish

Reputation: 520

String word = "test";
String[] vowels ={ "a", "e", "i", "o", "u"}
for (int i = (vowels.length - 1); i>=0; i-- ){

    word = word.replaceAll(vowel[i], vowel[i].concat("ub"));
}

Upvotes: 1

Related Questions