how to find index of first vowel in a string?

2019-09-06 06:14发布

问题:

This part i want implemented but don't know how: In Pig Latin, all the consonants until the first vowel should be moved to the end of the word. So while "laptop" will still be "aptoplay", "string" should become "ingStray".

I have this, the first part works just fine.

public static String doStuff(String word) {
    int number = 0;
    char[] vowel = { 'a', 'e', 'i', 'o', 'u' };
    char first = word.charAt(0);
    char second = Character.toLowerCase(first);
    if (second == 'a' || second == 'e' || second == 'i' || 
            second == 'o' || second == 'u') {
        word = word + "ay";
    } else {
        for (int i = 0; i < word.length(); i++) {
        for (int j = 0; j < 5; j++) {
            if (word.charAt(i) == vowel[j]) {
                word = word.substring(i + 1) + word.substring(i) + "ay";
                break;
            }
        }
    }
    return word;
}

回答1:

First of all, your break should break out of both loops. To do so, put a flag before your first loop like

outerloop:
for (int i = 0; i < word.length(); i++) {

Then instead of break; put break outerloop;

Second,

word = word.substring(i + 1) + word.substring(i) + "ay";

doesn't work, because when you find the vowel, you want to keep it in front, so i + 1 should be just i. Then, the rest of the substring before "ay" should be the consonants at the beginning, so 0, i instead of just i. In all, this gives

outerloop:
    for (int i = 0; i < word.length(); i++) {
        for (int j = 0; j < 5; j++) {
            if (word.charAt(i) == vowel[j]) {
                word = word.substring(i) + word.substring(0, i) + "ay";
                break outerloop;
            }
        }
    }


回答2:

Just walk the String until you hit the first vowel, which will give you the index value you need for String.substring()

Simply your doStuff() method to the following:

public static void main(String[] args) throws Exception {
    System.out.println(doStuff("laptop"));
    System.out.println(doStuff("String"));
    System.out.println(doStuff("apple"));
}

public static String doStuff(String word) {
    String vowels = "aeiouAEIOU";
    for (int i = 0; i < word.length(); i++) {
        if (vowels.contains(""+word.charAt(i))) {
            String prefix = word.substring(0, i);
            String suffix = word.substring(i);
            word = suffix + prefix + "ay";
            break;
        }
    }
    return word;
}

Results:

aptoplay
ingStray
appleay


回答3:

Check length of string, check if "aeiou" is one of the characters at the split point. If it is, reappend the string accordingly, else continue to next character.

String doStuff(string a)
{
    char c;
    string s = "aeiou" //y?
    int i,l;
    l=a.length();
    for(i = 0;i < l;i++)
    {   
        c=a.charAt(i);
        if(s.contains(c))
        break;
    }
    a=a.substring(i,l)+a.substring(0,i)+"ay";
    return a;
}


回答4:

I am kind of new out here so not much aware of the conventions. Anyhow, the code below should be able to help you achieve the desired conversion.

Using a list of vowels provides you the 'contains' operation which allows for easier to understand code as compared to nested loops. There is no advantage in terms of processing though.

In the following code, inp is the input string.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class piglatin {
    public static void main(String args[]) {
        String[] vowels = new String[] { "A", "a", "E", "e", "I", "i", "O",
                "o", "U", "u" };
        List<String> vowelList = new ArrayList<String>(Arrays.asList(vowels));
        String inp = "string";
        String returnString = inp;
        for (int i = 0; i < inp.length(); i++) {
            if(vowelList.contains(""+inp.charAt(i))){
                returnString = inp.substring(i)+inp.substring(0,i)+"ay";
                break;
            }
        }
        System.out.println(returnString);
    }

}

Edit : Didn't realize ".contains" could be used with Strings as well as done by Shar1er80 . Now ain't stack exchange all about learning ;)



回答5:

public static String pigLatin(String origianl){
            List<Character> vowels = new ArrayList<>(5);
            vowels.add('a');
            vowels.add('e');
            vowels.add('i');
            vowels.add('o');
            vowels.add('u');
            boolean vowelFound;
            for(int i = 0; i < origianl.length(); i++){
                if(vowels.contains(origianl.toLowerCase().charAt(i))){
                    String post = origianl.substring(0, i);
                    String pre = origianl.substring(i);
                    return pre + post + "ay";
                }
            }
            return null;
        }

You don't have to use the list. There are plenty of other options (like using a bunch of || in an if statement). Also, it's probably better to put the List outside the method so it doesn't need to be created every time the method is called if possible.