1

I am a newb practising vb code by attempting a piglatin translator. I'm stuck on the part where I need to send any consonant letters to the back of the word and loop this process until the first letter becomes a vowel. For example,

dragon--> loop once
Ragond--> loop again
Agondr--> the first word is a vowel- now stop!

How do I do this? I've attempted it in my code but it gives me nothing. Also, how would I keep correct case in this code? Dragon= Agondray DRAGON = AGONDRAY etc.

Public Class Form1



    Private Sub translatebtn_Click(sender As Object, e As EventArgs) Handles translatebtn.Click

        Dim wordlist() As String = englishtext.Text.Split(" ")   'this splits up the english word/sentence into an individual words.

        Dim i As Integer

        'this loops translation code through each word
        For i = 0 To wordlist.Length - 1
            piglatin(wordlist(i))
        Next


    End Sub


    Public Const Vowels As String = "aeiouyAEIOUY"
    Public Const upperconsonant As String = "BCDFGHJKLMNPQRSTVWXZ"
    Public Const consonant As String = "bcdfghjklmnpqrstvwxz"

    Public Function hasavowel(ByVal intheword As String) As Boolean  'the word has a vowel

        Dim i As Integer
        For i = 0 To 11
            If (intheword = Vowels(i)) Then
                Return True
            End If
        Next

        Return False

    End Function

    Public Function hasaconsonant(ByVal intheword As String) As Boolean 'the word has a consonant

        Dim i As Integer
        For i = 0 To 19
            If (intheword = consonant(i)) Then
                Return True
            End If
        Next

        Return False

    End Function





    Private Function moveLetter(ByVal strWord As String) As String

        Dim intheword As Char

        If hasavowel(strWord(0)) Then

            Return strWord                                      'this is for the vowel starting words


        Else
            Do While hasaconsonant(strWord(0))

                intheword = Char.ToLower(strWord(0))


            Loop
            Return moveLetter(strWord.Substring(1) + intheword)              'this is for the consonant starting words

        End If


    End Function


    Private Sub piglatin(ByVal strWord As String)
        If hasavowel(strWord(0)) Then
            piglatintext.Text += strWord + "way "  'if the vowel is in the first position of the word. way is added on end.

        Else

            piglatintext.Text += moveLetter(strWord) + "ay " 'otherwise, ad ay.
        End If

    End Sub

2 Answers2

0

You have a few recurring problems in your code:
1. You cant use MyString(pos) to get a character out of a string. Use either Mid(MyString,pos,1) or Left(MyString,1) to get a single character.
2. Indexes start at 1 in VBA
3. Return statements are not using the word Return. To set a return value, set the name of the function like this: hasaconsonant = False

Sam
  • 5,424
  • 1
  • 18
  • 33
0

Your loop should be updating strWord since you are using it to compare in the while.

Private Function moveLetter(ByVal strWord As String) As String

    If Not hasavowel(strWord(0)) Then
        Do While hasaconsonant(strWord(0))
            strWord = strWord.Substring(1) & strWord(0)
        Loop
    End If

    Return strWord
End Function
the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • Great! It worked thank you. Is there a possible method to keep titlecase and uppercase? For example Dragon = Agondr and DRAGON = AGRONDR. Can I introduce another 'else if' or something similar after that 'If Not hasavowel...' statement? – user9395865 Feb 22 '18 at 15:43
  • @user9395865 I think this would be a bit more complicated. I can't think of a simpler solution than having an array of case. If you ask an other question, maybe someone could help. – the_lotus Feb 22 '18 at 15:56