1

Ok so I got a small code on trying to convert english into Pig Latin, I got the vowels working but the consonants are more difficult, based on what i have i can only remove the first consonant, So the word "what" is translated into "hatway" instead of "atwhway" please help, I'm still new to VB this is what i have

Public Const Vowels As String = "aeiou"
Public Const Consonant As String = "bcdfghjklmnpqrstvwxyz"
Private Const ConsonantSuffix As String = "ay"

For consonantIndex As Integer = 0 To Consonant.Length - 1 Step 1
            If word.ToLower.StartsWith(Consonant(consonantIndex).ToString) Then
                word = word.Remove(0, 1)
                word = word & Consonant(consonantIndex) & ConsonantSuffix
            End If
        Next
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
OPJ
  • 123
  • 2
  • 13

1 Answers1

1

I'd suggest a different strategy. Find the index first vowel, and then rearrange the letters around that index.

Dim index = word.IndexOfAny(Vowels.ToArray)
If (index > 0) then
    word = word.Substring(index) & word.Remove(index)
End If
word &= ConsonantSuffix

You can make this slightly more efficient by storing the Vowels array statically rather than calling ToArray every time.

Examples (taken from the Wikipedia article on the subject):

  • 'happy' → 'appyhay'
  • 'duck' → 'uckday'
  • 'glove' → 'oveglay'
  • 'egg' → 'eggay'
  • 'inbox' → 'inboxay'
  • 'eight' → 'eightay'
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • You are a life saver!! Just tried it and it worked!! – OPJ Apr 15 '14 at 21:54
  • Wait, how about with a word like "Bob" with your method it comes out like "obBay" how would you get it to be "obbay" – OPJ Apr 15 '14 at 22:02
  • 1
    @OPJ Use `word.Remove(index).ToLower` instead of `word.Remove(index)`. – p.s.w.g Apr 15 '14 at 22:04
  • How would keep the orignal casing? Like "Bob" would be "Obbay" but then theres a word like "WHAT" how would i keep the casing to be "ATWHAY" – OPJ Apr 15 '14 at 22:13
  • 1
    @OPJ Well there are a few issues with that. You have to first figure out if this is a title case ("Bob") or an all caps word ("WHAT") and then apply the the capitalization rules accordingly. You'll also have to tweak your vowels array to accept both upper and lower case strings. Neither of those tasks are too hard, and I'm sure you'll find good solutions on Google or here, but it's probably outside the scope of this question. – p.s.w.g Apr 15 '14 at 22:19
  • I'm sorry last question, what would type on google for this, I'm sorry, i'm brain dead at the moment for all stress on this assignment – OPJ Apr 15 '14 at 22:22
  • 1
    @OPJ Well start with http://stackoverflow.com/questions/448206/detecting-if-a-string-is-all-caps and then see how you can figure out how use `Char.IsUpper` to test for title case as well as all caps. Then you can search for various solution to convert the word to title case (i.e. to capitalize word). Try it out yourself. If you get stuck, you can always come back here and ask another question. – p.s.w.g Apr 15 '14 at 22:29