Finding where the first vowel occurs

I am trying to write a program that translates English to PigLatin. I am currently trying to solve this part where the first vowel of a word can be found, that the program can cut the word correctly and change it correctly.

For example, the string "Hi, I'm a guy" becomes "ellhay Iyay amyay ayay uygay". (On the list, I think my pig is latin correct, this goes away from the example I created.

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

I thought to compare it to the string first, vowels = "aeiouy" and then from there, but I'm stuck. Here's what I have:

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

}

      

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

+3


source to share


4 answers


Here's one way:

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 just goes through the word character by character and checks each character to see if it exists in your vowel String.

+4


source


Why not use a mask representing the vowels in the ASCII table (not exaggerated)?

This is not the easiest solution, but it is really very fast because it uses bitwise operations.

Returns -1

if the vowel was not 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;
}

      

change

I forgot about the 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'

.

+3


source


You can take each of the characters of the string with the charAt () way

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;
}

      

+1


source


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;
}

      

0


source







All Articles