Question

I have to take a string and convert the string to piglatin. There are three rules to piglatin, one of them being:

if the english word starts with a vowel return the english word + "yay" for the piglatin version.

So i tried doing this honestly expecting to get an error because the startsWith() method takes a string for parameters and not an array.

 public String pigLatinize(String p){
    if(pigLatRules(p) == 0){
        return p + "yay";
    }
}

public int pigLatRules(String r){
    String vowel[] = {"a","e","i","o","u","A","E","I","O","U"};
    if(r.startsWith(vowel)){
        return 0;
    }        
}

but if i can't use an array i'd have to do something like this

if(r.startsWith("a")||r.startsWith("A")....);
     return 0;

and test for every single vowel not including y which would take up a very large amount of space, and just personally I would think it would look rather messy.

As i write this i'm thinking of somehow testing it through iteration.

 String vowel[] = new String[10];
 for(i = 0; i<vowel[]; i++){
     if(r.startsWith(vowel[i]){
         return 0;
     }

I don't know if that attempt at iteration even makes sense though.

Was it helpful?

Solution

Your code:

String vowel[] = new String[10];
for(i = 0; i<vowel[]; i++){
    if(r.startsWith(vowel[i]){
        return 0;
     }
}

Is actually really close to a solution that should work (assuming you actually put some values in the array).

What values do you need to put in it, well as you mentioned you can populate the array with all the possible values for vowels. Those of course being

String[] vowel={"a","A","e","E","i","I","o","O","u","U"};

now you have this you would want to loop (as you worked out) over the array and do your check:

public int pigLatRules(String r){
    final String[] vowels={"a","A","e","E","i","I","o","O","u","U"};
    for(int i = 0; i< vowels.length; i++){
        if(r.startsWith(vowels[i])){
             return 0;
         }
    }
    return 1;
}

There are some improvements you can make to this though. Some are best practice some are just choice, some are performance.

As for a best practice, You are currently returning an int from this function. You would be best to change the result of this function to be a boolean value (I recommend looking them up if you have not encountered them).

As for a choice you say you do not like having to have an array with the upercase and lowercase vowels in. Well here is a little bit of information. Strings have lots of methods on them http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html one of them is toLowerCase() which as you can guess lowercases a whole string. if you do this to the work you pass in to your function, you cut the amount of checks you need to do in half.

There is lots more you cam get into but this is just a little bit.

OTHER TIPS

Put all those characters in a HashSet and then just perform a lookup to see if the character is valid or not and return 0 accordingly.

Please go through some example on HashSet insert/lookup. It should be straightforward.

Hope this helps.

Put all the vowels in a string, grab the first char in the word you are testing and just see if your char is in the string of all vowels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top