Search code examples
stringvb.netif-statementlatin

pig latin practice on vb


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

Solution

  • 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