Hi all,

I have been playing around with trying to create an english to pig latin translator.

I am not really sure where to start, so right now I am just focusing on splitting a string and search for "A", if the word has "A" add "way" to the end of it. Here is what I figured out from some reasearch (I found most of this from a post on here):

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

       Dim strComplete As String = TextBox1.Text
       Dim strWords() As String
       strWords = strComplete.Split(" ")

       strWords = strComplete.Split(strSeparators)

        TextBox2.Text = String.Join("-", strWords) 'hypen used to tell me string is working. 

    End Sub
End Class

This works, the problem happens when I try to search the string and manipulate it. I was told to use the startswith command to do the manipulation.

So if I do something like:

If strWords.StartsWith("A") Then
           strWords += "-WAY"
       End If

It gives me an error that says "StartsWith' is not a member of 'System.Array", but if I do:

If strWords(0).StartsWith("A") Then
           strWords(0) += "-WAY"
       End If

It works, But I am not sure how to get it so it search the entire string without having to do 1,2,3,ect.

What would be the best way to search the string for certain letters? I was thinking some type of case or loop but I tried a few different things, but could not get it process correctly. Google, gives me alot of different examples, but I can not get most of them working with my code. I only have a semester of VB.net Programming, so I am trying to find the best way to do this, without making it too complicated.

Thanks!

I talked to my professor and he suggested I should be do a for next loop and a create a function.
Here is an updated code:

Public Class Form1
    Dim strOutput() As String
    Private Function translateword(ByVal w As String) As String

        If w.StartsWith("A") Then
            w += "-Way"
        End If

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

        Dim strComplete As String = TextBox1.Text
        Dim strWords() As String

        strWords = strComplete.Split(" ")  'use a space as the delineator

        For Each w As String In strWords
            translateword(w)
        Next


        TextBox2.Text = String.Join("-", w) 'hypen used to tell me string is working. 


    End Sub
End Class

But I am still not able to output the translation to textbox2, in this case with "w", I get an error that says w is not declared. If I try to manipulate it and try to do something like

Dim stroutput As Decimal

stroutput = w 
textbox2.text = stroutput

I get an error message about "value of type cannot be converted to a 1 dimensional array", my reasearch is failing me on how to fix this issue.

Thanks again.

Dim stroutput As Decimal

A Decimal is a Number, not Text. You need to declare it "As String" and include the value within the quotation marks.

Dim strOutput As String
        strOutput = "w"
        TextBox2.Text = strOutput

As for the rest, see if this helps.

Public Class Form1

    Private Function translateWord(ByVal selectedWord As String) As String
        '// use .ToLower in case there are LowerCase words that start with "a".
        If selectedWord.ToLower.StartsWith("a") Then selectedWord &= "-Way" '// use "&" when adding to a String.
        Return selectedWord
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c) '// .Split .Text into arrays.
        TextBox2.Clear()  '// Clear your TextBox for new input.
        For Each w As String In strWords '// loop thru all arrays.
            TextBox2.Text &= " " & translateWord(w) '// add space and word to TextBox.
        Next
    End Sub
End Class

Edited 5 Years Ago by codeorder: n/a

Comments
Descriptions on the code helped alot.

Codeorder,

Thank you very much, that helps a lot. Here is my new code with your changes, I added some more "adjustments" the strings but ran into some issues:

Public Class Form1

    Private Function translateWord(ByVal selectedWord As String) As String
        '// use .ToLower in case there are LowerCase words that start with "a".

        If selectedWord.ToLower.Contains("@") Then 'How do I check for other symbols?
            selectedWord = selectedWord
        ElseIf IsNumeric(selectedWord) Then  'Checks for numbers (working)
            selectedWord = selectedWord
        ElseIf selectedWord.ToLower.StartsWith("a") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        ElseIf selectedWord.ToLower.StartsWith("e") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        ElseIf selectedWord.ToLower.StartsWith("i") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        ElseIf selectedWord.ToLower.StartsWith("o") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        ElseIf selectedWord.ToLower.StartsWith("u") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        Else 'Next line checks for constants (not working, found this code from another post)
            selectedWord = Microsoft.VisualBasic.Right(selectedWord, 3) _
            & Microsoft.VisualBasic.Left(selectedWord, 2) & "ay"
        End If


        Return selectedWord
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c) '// .Split .Text into arrays.
        TextBox2.Clear()  '// Clear your TextBox for new input.
        For Each w As String In strWords '// loop thru all arrays.
            TextBox2.Text &= " " & translateWord(w) '// add space and word to TextBox.
        Next
    End Sub
End Class

There are some other things I am still struggling with:

• If a word starts with a consonant, move the consonants before the first vowel to the end of the word and add ay. - I have this semi working, but for a word like SQL or won't it doesn't work. For example won't translates to n'twoay when it should be ont'way or punctuation should be unctuationp

•How do I tell it not to translate words with symbols like shawn@web.com? I put @ in the first "check" but is there an easy way to do other symbols?

Edited 5 Years Ago by shawn130c: added examples for first bullet

Ok making a little progress, but then find other issues.

To do the consonants I add this code to the translate function:

Dim chLetter As Char
chLetter = Char.ToString(selectedWord(0))
selectedWord = selectedWord.Substring(1) + chLetter + "ay"
'Still having a problem with uppercase and lowercase and punctuation

This seems to work, but having issues with the capitlization. For example "This" should be "Isthay" but I get "hisTay" so it is not moving the h and capatilzing correctly.

Also noticed that punctuations like Apple. (period at end) isnt moving the period to the end.

Updated Code:

Public Class Form1

    Private Function translateWord(ByVal selectedWord As String) As String
        '// use .ToLower in case there are LowerCase words that start with "a".

        If selectedWord.ToLower.Contains("@") Then
            selectedWord = selectedWord
        ElseIf IsNumeric(selectedWord) Then
            selectedWord = selectedWord
        ElseIf selectedWord.ToLower.StartsWith("a") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        ElseIf selectedWord.ToLower.StartsWith("e") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        ElseIf selectedWord.ToLower.StartsWith("i") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        ElseIf selectedWord.ToLower.StartsWith("o") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        ElseIf selectedWord.ToLower.StartsWith("u") Then
            selectedWord &= "way" '// use "&" when adding to a String.
        Else

            Dim chLetter As Char
            chLetter = Char.ToString(selectedWord(0))
            selectedWord = selectedWord.Substring(1) + chLetter + "ay"
            'Still having a problem with uppercase and lowercase and punctuation 

        End If

        Return selectedWord
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c) '// .Split .Text into arrays.
        TextBox2.Clear()  '// Clear your TextBox for new input.
        For Each w As String In strWords '// loop thru all arrays.
            TextBox2.Text &= " " & translateWord(w) '// add space and word to TextBox.
        Next
    End Sub
End Class

Glad I could help and thanks for the detailed information on how to get the consonant words sorted.

See if this helps.

Private iIndex As Integer = 0 '// used to determine where to start the .Substring if a word starts with a Consonant.

    Private Function translateWord(ByVal selectedWord As String) As String
        With selectedWord.ToLower '// use the "With" statement to shorten code.
            If .Contains("@") Then 'How do I check for other symbols?
                selectedWord = selectedWord
            ElseIf IsNumeric(selectedWord) Then  'Checks for numbers (working)
                selectedWord = selectedWord
            ElseIf .StartsWith("a") OrElse .StartsWith("e") OrElse .StartsWith("i") OrElse .StartsWith("o") OrElse .StartsWith("u") Then
                selectedWord &= "way"
            Else '// Next line checks for constants.
                iIndex = 0 '// reset.
                For Each c As Char In selectedWord '// loop thru all Characters one at a time.
                    '// check if char. is a vowel.
                    If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" Then
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "ay"
                        Exit For '// Exit the loop once locating a vowel.
                    End If
                    iIndex += 1 '// increase +1 for next letter.
                Next
            End If
        End With
        Return selectedWord
    End Function

As for special char.s like "!@#$%", what do you plan to do with those?

If pig latin is for pigs and latin is for humans, then if you have never used either, what are you?:D

Codeorder,

Since I have never used piglatin, I must be a college student you can't spell latin (see thread title...) :)

Thank you very much for the constant fixing and code cleanup. Learned alot from it, especially those orelse commands.

As for special char.s like "!@#$%", my instructions are the leave as is, unless it is ending puncutation. I just added this to the code, which should work:

ElseIf .Contains("@") OrElse .Contains("%") OrElse .Contains("$") OrElse .Contains("%") Then
                selectedWord = selectedWord

That code seemed to fixed most of the issues, but still having issues with the following:

1. It seems like the code now doesn't like uppsercase words. Ex: CASE remains CASE

2. Adjusting the case of the string. Ex: Hello should be Ellohay or CASE should be ASECAY, though Hello is elloHay.
-I tried messing around with the ToUpper and ToLower, but no luck. I found this command:
VbStrConv.ProperCase but not sure where to put it.

Worst case, if we can't figure out the case, as long as I can make everything lowercase it might be ok.

3. Ending punctuation. Ex: apple. should be ppleaway. but I am getting apple.way . I was thinking string remove command but not 100% sure.

4. This is me being nitpicky but I noticed a space in the beginning of the text in textbox2 (output). Is there a way to take that out. I am thinking the Trim command, but the few places I put it doesn't work.

Still trying some things, but here is an updated code, with the addition of the special characters.

Though thanks for all the help. I have been playing with this code for awhile and would have never gotten this far, without you. More importantly, I am learning some real cool commands I never knew about.

Public Class Form1

    Private iIndex As Integer = 0 '// used to determine where to start the .Substring if a word starts with a Consonant.
    Private Function translateWord(ByVal selectedWord As String) As String
        With selectedWord.ToLower '// use the "With" statement to shorten code.

            If IsNumeric(selectedWord) Then
                selectedWord = selectedWord
            ElseIf .Contains("@") OrElse .Contains("%") OrElse .Contains("$") OrElse .Contains("%") Then
                selectedWord = selectedWord
            ElseIf .StartsWith("a") OrElse .StartsWith("e") OrElse .StartsWith("i") OrElse .StartsWith("o") OrElse .StartsWith("u") Then
                selectedWord &= "way"
            Else '// Next line checks for constants.
                iIndex = 0 '// reset.
                For Each c As Char In selectedWord '// loop thru all Characters one at a time.
                    '// check if char. is a vowel.
                    If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" Then
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "ay"
                        Exit For '// Exit the loop once locating a vowel.
                    End If
                    iIndex += 1 '// increase +1 for next letter.
                Next
            End If
        End With
        Return selectedWord
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c) '// .Split .Text into arrays.
        TextBox2.Clear()  '// Clear your TextBox for new input.
        For Each w As String In strWords '// loop thru all arrays.
            TextBox2.Text &= " " & translateWord(w) '// add space and word to TextBox.
        Next
    End Sub
End Class

Edited 5 Years Ago by shawn130c: n/a

What about...

Return selectedWord.ToLower

Does not take care of the UpperCase letters, but it is a start for cleaning up.

As for the TextBox and the unknown case of the "empty space" at the beginning, check if TextBox has .Text, if not, do not add " ", else do.

For Each w As String In strWords '// loop thru all arrays.
            '// Since more than likely you will get more than one word in the TextBox, start with "If Not ='s "".
            If Not TextBox2.Text = "" Then TextBox2.Text &= " " & translateWord(w) Else TextBox2.Text = translateWord(w)
        Next

I'm quite busy for the rest of the day with beta testing software for a friend here at DaniWeb, but if you would like to check out another thread that has a String manipulation with keeping the end punctuation, check out this thread.
Let me know if that link helps, if not I'll see what I can do.

Edited 5 Years Ago by codeorder: n/a

Codeorder,

Thanks for spending all this time on this, I really appreciate it. I made some progress today.

1. I semi fixed the cap issue by add the tolower in the consonants part. With the code below it now is back to translating CASE to ASECay, instead of ignoring it.

-From here I am still playing with how to capitalize the ay, thinking of just adding another if statement.

For Each c As Char In selectedWord.ToLower

2. The code for the removing spacing worked nicely

Only items left:

1. Adjusting the case of the string. Ex: Hello should be Ellohay or CASE should be ASECAY, though Hello is elloHay.

-I was thinking another way to tackle it would be to "cheat" and make it so if the first two letters are capitol consonants it makes ay capitol and do something similar with the capitalization in the beginning.


Worst case, if we can't figure out the case, as long as I can make everything lowercase it might be ok.

2. Ending punctuation. Ex: apple. should be ppleaway. but I am getting apple.way . I was thinking string remove command but not 100% sure.

Updated Code:

Public Class Form1

    Private iIndex As Integer = 0 '// used to determine where to start the .Substring if a word starts with a Consonant.
    Private Function translateWord(ByVal selectedWord As String) As String
        With selectedWord.ToLower '// use the "With" statement to shorten code.

            If IsNumeric(selectedWord) Then
                selectedWord = selectedWord
            ElseIf .Contains("@") OrElse .Contains("%") OrElse .Contains("$") OrElse .Contains("%") Then
                selectedWord = selectedWord
            ElseIf .StartsWith("a") OrElse .StartsWith("e") OrElse .StartsWith("i") OrElse .StartsWith("o") OrElse .StartsWith("u") Then
                selectedWord &= "way"
                'Backup For CAPS, ADD A,E,I,O,U
            Else '// Next line checks for constants.
                iIndex = 0 '// reset.
                'For Each c As Char In selectedWord '// add to lowerloop thru all Characters one at a time.
                For Each c As Char In selectedWord.ToLower
                    '// check if char. is a vowel.
                    If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" Then
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "ay"
                        Exit For '// Exit the loop once locating a vowel.
                    End If
                    iIndex += 1 '// increase +1 for next letter.
                Next
            End If
        End With
        Return selectedWord

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c) '// .Split .Text into arrays.
        TextBox2.Clear()  '// Clear your TextBox for new input.

        'For Each w As String In strWords '// loop thru all arrays.
        '    TextBox2.Text &= " " & translateWord(w) '// add space and word to TextBox.
        'Next

        For Each w As String In strWords '// loop thru all arrays.
            '// Since more than likely you will get more than one word in the TextBox, start with "If Not ='s "".
            If Not TextBox2.Text = "" Then TextBox2.Text &= " " & translateWord(w) Else TextBox2.Text = translateWord(w)
        Next

    End Sub
End Class

Thanks!

See if this helps for punctuation and one too many ProperCases:D.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c)
        TextBox2.Clear()
        For Each w As String In strWords
            If w.Length > 1 Then '// check if word length is greater than 1.
                If Char.IsLetterOrDigit(CChar(w.Substring(w.Length - 1, 1))) Then '// check if last char. is Letter or a Number.
                    If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & StrConv(translateWord(w), vbProperCase)
                    Else : TextBox2.Text = StrConv(translateWord(w), vbProperCase)
                    End If
                Else '// if NOT last char. is Letter or a Number.
                    If Not TextBox2.Text = "" Then
                        '// send only the word without the last char. to the Function and add the last char. back to the word.
                        TextBox2.Text &= " " & StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    Else
                        TextBox2.Text = StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    End If
                End If
            End If
        Next
    End Sub
End Class

I ended up using "vbProperCase" for all Function Returns, but you only need to use it for words that first letter is capitalized.
.Give it a try and reply back if you succeed or need further help.

About not knowing how to use the "ProperCase", see if this helps.

Dim sTemp As String = "check me out, i'm going to be proper cased :D"
        MsgBox(StrConv(sTemp, vbProperCase))

Edited 5 Years Ago by codeorder: n/a

O wow that code is very nice, and is very very close. But for some reason it takes out I if it is a single word and capitalize every word, instead of the first. I am wondering if I is being moved/erased because it is a vowel.

Though I am still messing around with some changes. I am thinking maybe if I add .tolower to button code it might help with the caps issue. "I" issue is still kind of strange though.

Example:

My name is Shawn. I love this forum.

Translates to:

My Amenay Isway Awnshay. Ovelay Isthay Orumfay.

-Missing I/Iway and all first are caps, though the periods are perfect.

Also while testing I notice that why and my are not being translated, this isn't related to the most recent changes, I just recreated the code from one of the earlier posts and it does the same thing. Weird...

Here is the updated code:

Public Class Form1


    Private iIndex As Integer = 0 '// used to determine where to start the .Substring if a word starts with a Consonant.
    Private Function translateWord(ByVal selectedWord As String) As String
        With selectedWord.ToLower '// use the "With" statement to shorten code.

            If IsNumeric(selectedWord) Then
                selectedWord = selectedWord
            ElseIf .Contains("@") OrElse .Contains("%") OrElse .Contains("$") OrElse .Contains("%") Then
                selectedWord = selectedWord
            ElseIf .StartsWith("a") OrElse .StartsWith("e") OrElse .StartsWith("i") OrElse .StartsWith("o") OrElse .StartsWith("u") Then
                selectedWord &= "way"
                'Backup For CAPS, ADD A,E,I,O,U
            Else '// Next line checks for constants.
                iIndex = 0 '// reset.
                'For Each c As Char In selectedWord '// add to lowerloop thru all Characters one at a time.
                For Each c As Char In selectedWord.ToLower
                    '// check if char. is a vowel.
                    If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" Then
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "ay"
                        Exit For '// Exit the loop once locating a vowel.
                    End If
                    iIndex += 1 '// increase +1 for next letter.
                Next
            End If
        End With
        Return selectedWord

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c)
        TextBox2.Clear()
        For Each w As String In strWords
            If w.Length > 1 Then '// check if word length is greater than 1.
                If Char.IsLetterOrDigit(CChar(w.Substring(w.Length - 1, 1))) Then '// check if last char. is Letter or a Number.
                    If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & StrConv(translateWord(w), vbProperCase)
                    Else : TextBox2.Text = StrConv(translateWord(w), vbProperCase)
                    End If
                Else '// if NOT last char. is Letter or a Number.
                    If Not TextBox2.Text = "" Then
                        '// send only the word without the last char. to the Function and add the last char. back to the word.
                        TextBox2.Text &= " " & StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    Else
                        TextBox2.Text = StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    End If
                End If
            End If
        Next
    End Sub
End Class

Edited 5 Years Ago by shawn130c: Added Examples

Small victory... I believe the issue with "I" was this line, so I added =, though not sure how this really changes the code.

If w.Length >= 1 Then '// check if word length is greater than 1.

Now it is:

My name is CASE. I love this forum and won't leave, can't wait until we are done.

My Amenay Isway Asecay. Iway Ovelay Isthay Orumfay Andway On'tway Eavelay, An'tcay Aitway Untilway Eway Areway Oneday.

Still working with My, Why and the uppercase for each word issues.


Updated Code:

Public Class Form1


    Private iIndex As Integer = 0 '// used to determine where to start the .Substring if a word starts with a Consonant.
    Private Function translateWord(ByVal selectedWord As String) As String
        With selectedWord.ToLower '// use the "With" statement to shorten code.

            If IsNumeric(selectedWord) Then
                selectedWord = selectedWord
            ElseIf .Contains("@") OrElse .Contains("%") OrElse .Contains("$") OrElse .Contains("%") Then
                selectedWord = selectedWord
            ElseIf .StartsWith("a") OrElse .StartsWith("e") OrElse .StartsWith("i") OrElse .StartsWith("o") OrElse .StartsWith("u") Then
                selectedWord &= "way"
                'Backup For CAPS, ADD A,E,I,O,U
            Else '// Next line checks for constants.
                iIndex = 0 '// reset.
                'For Each c As Char In selectedWord '// add to lowerloop thru all Characters one at a time.
                For Each c As Char In selectedWord.ToLower
                    '// check if char. is a vowel.
                    If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" Then
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "ay"
                        Exit For '// Exit the loop once locating a vowel.
                    End If
                    iIndex += 1 '// increase +1 for next letter.
                Next
            End If
        End With
        Return selectedWord

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c)
        TextBox2.Clear()
        For Each w As String In strWords
            If w.Length >= 1 Then '// check if word length is greater than 1.
                If Char.IsLetterOrDigit(CChar(w.Substring(w.Length - 1, 1))) Then '// check if last char. is Letter or a Number.
                    If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & StrConv(translateWord(w), vbProperCase)
                    Else : TextBox2.Text = StrConv(translateWord(w), vbProperCase)
                    End If
                Else '// if NOT last char. is Letter or a Number.
                    If Not TextBox2.Text = "" Then
                        '// send only the word without the last char. to the Function and add the last char. back to the word.
                        TextBox2.Text &= " " & StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    Else
                        TextBox2.Text = StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    End If
                End If
            End If
        Next
    End Sub
End Class

Edited 5 Years Ago by shawn130c: n/a

I think we are almost there. By making this change, it gets close:

If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & StrConv(translateWord(w), vbProperCase)

to

If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & translateWord(w)

Here is what happens:


Hello my name is SHAWN. I can't get this working. But won't give up.

I get this:

Ellohay my amenay isway Awnshay. Iway an'tcay etgay isthay Orkingway. utBay on'tway ivegay Upway.

So the only few things left are

1. Some capitalization still wrong.

2. my why issue

2. problems with case. Example: CASE should be ASECAY not Asecay.

New Code:

Public Class Form1


    Private iIndex As Integer = 0 '// used to determine where to start the .Substring if a word starts with a Consonant.
    Private Function translateWord(ByVal selectedWord As String) As String
        With selectedWord.ToLower '// use the "With" statement to shorten code.

            If IsNumeric(selectedWord) Then
                selectedWord = selectedWord
            ElseIf .Contains("@") OrElse .Contains("%") OrElse .Contains("$") OrElse .Contains("%") Then
                selectedWord = selectedWord
            ElseIf .StartsWith("a") OrElse .StartsWith("e") OrElse .StartsWith("i") OrElse .StartsWith("o") OrElse .StartsWith("u") Then
                selectedWord &= "way"
            Else '// Next line checks for constants.
                iIndex = 0 '// reset.
                'For Each c As Char In selectedWord '// add to lowerloop thru all Characters one at a time.
                For Each c As Char In selectedWord.ToLower
                    '// check if char. is a vowel.
                    If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" Then
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "ay"
                        Exit For '// Exit the loop once locating a vowel.
                    End If
                    iIndex += 1 '// increase +1 for next letter.
                Next
            End If
        End With
        Return selectedWord

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c)
        TextBox2.Clear()
        For Each w As String In strWords
            If w.Length >= 1 Then '// check if word length is greater than 1.
                If Char.IsLetterOrDigit(CChar(w.Substring(w.Length - 1, 1))) Then '// check if last char. is Letter or a Number.

                    'If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & StrConv(translateWord(w), vbProperCase)
                    If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & translateWord(w)

                    Else : TextBox2.Text = StrConv(translateWord(w), vbProperCase)

                    End If
                Else '// if NOT last char. is Letter or a Number.
                    If Not TextBox2.Text = "" Then
                        '// send only the word without the last char. to the Function and add the last char. back to the word.
                        TextBox2.Text &= " " & StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    Else
                        TextBox2.Text = StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    End If
                End If
                End If
        Next
    End Sub
End Class

This: If w.Length > 1 Then will check for words that have "only" more than one characters, and this: If w.Length >= 1 Then will check for words that have one character "and more".

It was an oversight on my part, did not thoroughly test it.

Edited 5 Years Ago by codeorder: n/a

No worries about not testing it. It is my job to thoroughly test it, you have already done more than enough for me and I do not expect you to test it completely.

I have made progress with the My why issues. I was given this instructions but did not pay attention to it:

If a word starts with the letter Y, the Y should be treated as a consonant. If the Y appears anywhere else in the word, it should be treated as a vowel.

Remember that we need to do this to consonants:

If a word starts with a consonant, move the consonants before the first vowel to the end of the word and
add ay.


So I add c = "y" to the consonant part of the function so now the line is:

If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" OrElse c = "y" Then

That fixed it most of examples. I am still having issues with "you" it becomes Youay instead of ouyay

so now we get this (i took out the changes I made to the propercase, it seemed to start capitalizing at weird points.)

Ellohay Ymay Amenay Isway Asecay. Iway Ovelay Isthay Orumfay. Iway On'tway Ivegay Upway! Ymay Amenay Isway Awnshay.

Instead of this:

Ellohay ymay amenay isway ASECAY. Iway ovelay isthay orumfay.
Iway on'tway ivegay upway! Ymay amenay isway Awnshay.


For this sentence:

Hello my name is CASE. I love this forum. I won't give up! My name is Shawn.

So I believe the only two issues left is the capitalization of every word, instead of first words and words that have caps in it. and the problem with y in some word. my correctly translates to myay but you becomes Youay instead of ouyay.

Also if this helps I found this online pig latin translator that is close to the way I need it:

http://users.snowcrest.net/donnelly/piglatin.html

It doesnt do emails correctly, but I already have that fixed.

Updated Code:

Public Class Form1


    Private iIndex As Integer = 0 '// used to determine where to start the .Substring if a word starts with a Consonant.
    Private Function translateWord(ByVal selectedWord As String) As String
        With selectedWord.ToLower '// use the "With" statement to shorten code.

            If IsNumeric(selectedWord) Then
                selectedWord = selectedWord
            ElseIf .Contains("@") OrElse .Contains("%") OrElse .Contains("$") OrElse .Contains("%") Then
                selectedWord = selectedWord
            ElseIf .StartsWith("a") OrElse .StartsWith("e") OrElse .StartsWith("i") OrElse .StartsWith("o") OrElse .StartsWith("u") Then
                selectedWord &= "way"
            Else '// Next line checks for constants.
                iIndex = 0 '// reset.

                For Each c As Char In selectedWord.ToLower '// added tolower used to lowerloop thru all Characters one at a time.
                    '// check if char. is a vowel.

                    If c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" OrElse c = "y" Then 'added c="y"
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "ay"
                        Exit For '// Exit the loop once locating a vowel.
                    End If
                    iIndex += 1 '// increase +1 for next letter.
                Next
            End If
        End With
        'Return selectedWord
        Return selectedWord

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strWords() As String = TextBox1.Text.Split(" "c)
        TextBox2.Clear()
        For Each w As String In strWords
            If w.Length >= 1 Then '// check if word length is greater than 1.
                If Char.IsLetterOrDigit(CChar(w.Substring(w.Length - 1, 1))) Then '// check if last char. is Letter or a Number.

                    If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & StrConv(translateWord(w), vbProperCase)
                        ' my edit If Not TextBox2.Text = "" Then : TextBox2.Text &= " " & translateWord(w)

                    Else : TextBox2.Text = StrConv(translateWord(w), vbProperCase)

                    End If
                Else '// if NOT last char. is Letter or a Number.
                    If Not TextBox2.Text = "" Then
                        '// send only the word without the last char. to the Function and add the last char. back to the word.
                        TextBox2.Text &= " " & StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    Else
                        TextBox2.Text = StrConv(translateWord(w.Substring(0, w.Length - 1)) & w.Substring(w.Length - 1, 1), vbProperCase)
                    End If
                End If
            End If
        Next
    End Sub
End Class

Thanks!

Edited 5 Years Ago by shawn130c: I was wrong about y, you still not working.

I have been playing around with the code and had a few ideas but could not figure out a way to execute them.

If it is too difficult to the fix the vbpropercase stuff, My professor recommending going back to the basic code before we added the vbpropercase and then trimming the word (take out punctuation), and then look for any punctuation at the end (just look for anything that's not A-Z and a-z) translate the word, and then append the punctuation back in.


But, I am not sure the best way to do this and keep the capitalization, the main issue was with words that start with cap, but not all uppercase.

I was think about it some more and made some changes. I think it is working exactly the way I want though I am still testing it.

But using a test paragraph:

This application will translate the English text you enter into Pig Latin. It will retain CASE and punctuation. It will also translate contractions, but it won't translate words that contain numbers or symbols, such as 86 or bill@microsoft.com.

I get this translation, that matches the translation I am given:

Isthay applicationway illway anslatetray ethay Englishway exttay ouyay enterway intoway Igpay Atinlay. Itway illway etainray ASECAY andway unctuationpay. Itway illway alsoway anslatetray ontractionscay, utbay itway on'tway anslatetray ordsway atthay ontaincay umbersnay orway olssymbay, uchsay asway 86 orway bill@microsoft.com.


Here is the code, though even though I might have the final code I still would like to see any adjustments anyone might have regarding the ability to cleanup the "basic" code. I do not really like the way I did the caps to figure out how to do AY or ay,

Public Class Form1

    Private iIndex As Integer = 0 '// used to determine where to start the .Substring if a word starts with a Consonant.

    Private Function translateWord(ByVal selectedWord As String) As String

        Dim hasperiod As Boolean = False

        If selectedWord.EndsWith(".") Then
            hasperiod = True
        End If

        Dim hascomma As Boolean = False

        If selectedWord.EndsWith(",") Then
            hascomma = True
        End If

        Dim hassemicolon As Boolean = False

        If selectedWord.EndsWith(";") Then
            hassemicolon = True
        End If

        selectedWord = selectedWord.Trim(".")
        selectedWord = selectedWord.Trim(",")

        Dim wordstartswithcap As Boolean

        If StrConv(selectedWord, VbStrConv.ProperCase) = selectedWord Then
            wordstartswithcap = True
        End If

        'With selectedWord.ToLower '// use the "With" statement to shorten code

        With selectedWord
            If IsNumeric(selectedWord) Then
                selectedWord = selectedWord
            ElseIf .Contains("@") OrElse .Contains("%") OrElse .Contains("$") OrElse .Contains("%") Then
                selectedWord = selectedWord
            ElseIf .StartsWith("a") OrElse .StartsWith("e") OrElse .StartsWith("i") OrElse .StartsWith("o") OrElse .StartsWith("u") Then
                selectedWord &= "way"

            ElseIf .StartsWith("A") OrElse .StartsWith("E") OrElse .StartsWith("I") OrElse .StartsWith("O") OrElse .StartsWith("U") Then
                selectedWord &= "WAY"

            Else '// Next line checks for constants.
                iIndex = 0 '// reset.

                ' orginal For Each c As Char In selectedWord '// added tolower used to lowerloop thru all Characters one at a time.
                '// check if char. is a vowel.
                For Each c As Char In selectedWord

                    If c = "A" OrElse c = "E" OrElse c = "I" OrElse c = "O" OrElse c = "U" Then 'added c="y"OrElse c = "Y"
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "AY"
                        Exit For '// Exit the loop once locating a vowel.

                    ElseIf c = "a" OrElse c = "e" OrElse c = "i" OrElse c = "o" OrElse c = "u" Then 'added c="y"OrElse c = "y"
                        '// get .Substring from location of vowel to end of string "&" add .Substring from start, with length of iIndex "&" add your "ay" to the end.
                        selectedWord = selectedWord.Substring(iIndex) & selectedWord.Substring(0, iIndex) & "ay"
                        Exit For '// Exit the loop once locating a vowel.
                    End If
                    iIndex += 1 '// increase +1 for next letter.
                Next
            End If
        End With

        If hasperiod = True Then
            selectedWord &= "."
        End If

        If hascomma = True Then
            selectedWord &= ","
        End If

        If hassemicolon = True Then
            selectedWord &= ";"
        End If


        If wordstartswithcap = True Then
            selectedWord = StrConv(selectedWord, VbStrConv.ProperCase)
        End If
        Return selectedWord

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

        Dim strWords() As String = TextBox1.Text.Split(" "c)

        TextBox2.Clear()


        For Each w As String In strWords '// loop thru all arrays.
            '// Since more than likely you will get more than one word in the TextBox, start with "If Not ='s "".
            If Not TextBox2.Text = "" Then TextBox2.Text &= " " & translateWord(w) Else TextBox2.Text = translateWord(w)
        Next

    End Sub
End Class

Goodnight :)

Thank you for all the help codeorder. I made some adjustments to the cleaned up code and it seems to be working. I have marked this thread solved.

This question has already been answered. Start a new discussion instead.