Jack Armstrong
Jack Armstrong

Reputation: 1239

Finding where the first vowel in a word occurs

I am trying to write a program that translates English into PigLatin. I am currently trying to solve the part on where to locate the first vowel of the word that way the program can properly slice the word and rearrange it properly.

For example, the string "hello I am a guy" becomes "ellohay Iyay amyay ayay uygay". (At list I think my pig latin is correct, this is going off an example I created.

So for the word "what" becomes "atwhay". The program finds that the first vowel is in slot 2, then gives me that integer,2.

I was thinking of first comparing it to a string, vowels="aeiouy" and then go from there but I am stuck. Here is what I have:

public static int indexOfFirstVowel(String word){
   int index=0;
   String vowels="aeiouy";
   return index;

}

In theory index will update to where the first vowel is.

Upvotes: 3

Views: 13069

Answers (4)

Pier-Alexandre Bouchard
Pier-Alexandre Bouchard

Reputation: 5245

Why not using a mask representing the vowels in the ASCII table (not extented)?

It's not the simplest solution, but it's actually really fast because it use bitwise operations.

It returns -1 if no vowel is found.

public static int indexOfFirstVowel(String aString) {
    for (int i = 0; i < aString.length(); i++) {
        char c = aString.charAt(i);

        if ((c > 64) & ((0x110411101104111L & (1L << (c - 65))) > 0)) {
            return i;
        }
    }

    return -1;
}

edit:

I forgot the extented ASCII table.

replace:

if ((c > 64) & ((0x110411101104111L & (1L << (c - 65))) > 0))

by

if ((c > 64) & (c <= 121) & ((0x110411101104111L & (1L << (c - 65))) > 0))

where 121 is the ascii code for 'y'.

Upvotes: 3

furkle
furkle

Reputation: 5059

Here's one way you could do it:

final static String vowels = "aeiouy";
public static int indexOfFirstVowel(String word){
    String loweredWord = word.toLowerCase();

    for (int index = 0; index < loweredWord.length(); index++)
    {
        if (vowels.contains(String.valueOf(loweredWord.charAt(index))))
        {
            return index;
        }
    }

    // handle cases where a vowel is not found
    return -1;
}

This simply walks through the word by character, and checks each character to see if it exists within your vowels String.

Upvotes: 5

Michail Michailidis
Michail Michailidis

Reputation: 12191

You can take each of the characters of a String with the method charAt()

public static int englishVowelIndexOf(String word){
    char[] vowels = {'a','e','o','i','u','y'};
    String wordLowered =  word.toLowerCase();
    for (int i=0; i < wordLowered.length(); i++){
        for (int j=0; j < vowels.length(); j++) {
           if (wordLowered.charAt(i) == vowels[j]){
              return i;
           }
        }
    }
    return -1;
}

Upvotes: 1

john abrhm
john abrhm

Reputation: 15

public static int indexOfFirstVowel(String word){
   int i;
    for(i=0; i<word.length();i++){  
     switch(word.charAt(i)){
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'y':
        return i;
     }
    }
   return -1;
}

Upvotes: 0

Related Questions