4

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
user1793532
  • 53
  • 1
  • 1
  • 4
  • your second method is good. what is the problem with that? just replace the "i – Ashwin Nov 02 '12 at 08:01
  • This worked fine, but only when i entered in a string that starts with "a", not a capital A or any of the other vowels, it appears it's only checking the first string in the array :(. Like when i enter in "are you" it returns "areyay youay" which i expect, but when i tried "Is your" is returned "Isay youray" when it should have returned "Isyay youray". – user1793532 Nov 02 '12 at 12:47
  • It works fine for me. It return "Isyay" for "Is". Please post your present code in the question. – Ashwin Nov 03 '12 at 02:56
  • I fixed it. public int pigLatRules(String r){ String[] vowel = {"a","A","e","E","i","I","o","O","u","U"}; for(int i=0; i – user1793532 Nov 04 '12 at 11:00
  • i guess the 4 space thing doesn't work for comments :\ – user1793532 Nov 04 '12 at 11:01
  • As soon as java encounters return statement, the control transfers back to the next line of the "calling" statement. So once return 0 is encountered in the for loop, the loop does not continue anymore. But good that you have found out your own solution, Best of luck with java:) – Ashwin Nov 04 '12 at 11:23

3 Answers3

2

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.

Paul Harris
  • 5,769
  • 1
  • 25
  • 41
  • I commented on an earlier post about this exact information, and it appears that it's only checking the first value of the array, or "a", because when i tried "are you" it returned "areyay youay", which is what i expected and need to get, but when i tried a capital a or any other vowel, for example "Is your" returned "Isay youray" when it should have returned "Isyay youray" it seems that it's only testing the vowel[0] String and then skipping the rest, or maybe idk, this is all really confusing and i'm suprised i got that close to the right answer on the first iterative method i've made. – user1793532 Nov 02 '12 at 14:50
  • I actually tested the code i wrote and it worked fine so im surprised you had any issue. – Paul Harris Nov 07 '12 at 23:07
1

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.

Vaibhav Desai
  • 2,618
  • 1
  • 16
  • 16
0

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.

John3136
  • 28,809
  • 4
  • 51
  • 69