Hello everyone,


I have to create pig latin game which I did and it is working. The problem is I coded it so it can translate one word. I need to know how can I make it translate a sentence that I don't know how many words in it.

Here is my code for the one word translation:

Public Class Form1



    Private Sub Convert_to_Pig_Latin(ByVal strWordToConvert As String, ByRef strPigLatinWord As String)

        If strWordToConvert.Substring(0, 1).ToUpper Like "[AEIOU]" Then
            strPigLatinWord = strWordToConvert & "-way"

        End If


        Dim strTemp As String = ""

        If strWordToConvert.Substring(0, 1).ToUpper Like "[!AEIOU]" Then



            Do While strWordToConvert <> ""

                strTemp = strTemp + strWordToConvert(0)
                strWordToConvert = strWordToConvert.Remove(0, 1)

                If strWordToConvert.ToUpper Like "[AEIOUY]*" Then
                    strPigLatinWord = strWordToConvert & "-" & strTemp & "ay"
                    Exit Sub

                End If
            Loop

        End If



        If strTemp.ToUpper Like "*[!AEIOUY]*" Then
            strPigLatinWord = strTemp & "-way"
        End If



    End Sub














    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strOriginalWord As String
        Dim strPigLatin As String

        
        strOriginalWord = TextBox1.Text.Trim


        Call Convert_to_Pig_Latin(strOriginalWord, strPigLatin)

        Label1.Text = strPigLatin



       










    End Sub
End Class

Thank you


Regards

I would suggesto to do some changes in the Button1_Click sub like:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim strOriginalWords As String()
    Dim strPigLatin As String
        
    strOriginalWords = TextBox1.Text.Trim.Split(" "c)
    Label1.Text = ""
    ForEach strWord as String in strOriginalWords
        Call Convert_to_Pig_Latin(strWord, strPigLatin)
        Label1.Text &= strPigLatin & " "
        strPigLatin = ""
    Next
    Label1.Text = Label1.Text.Trim
    Label1.Refresh()
End Sub

Another suggestion will change the Convert_to_Pig_Latin sub into function.

This is an untested code, but hope it helps.

This article has been dead for over six months. Start a new discussion instead.