I'm a complete newb at vb.net, practising by making a piglatin translator. The rules are working fine (if starts with vowel, add 'way' at end. If starts with consonant, take any consonants before the first vowel to the end of the word, finish off with 'ay')
e.g. earwig-->earwigway
plague-->agueplay
However I was wondering how would I keep the correct case.
e.g. EARWIG-->EARWIGWAY (all caps)
Plague-->Agueplay (title case)
Could I input something under 'Private Function moveletter...' to implement correct case in my code? Or would I have to introduce something else like arrays? This is what I have so far...
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 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 'could titlecase/uppercase code be here?
If Not hasavowel(strWord(0)) Then
Do While hasaconsonant(strWord(0))
strWord = strWord.Substring(1) & strWord(0)
Loop
End If
Return strWord
'this is for the consonant starting words
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
Aucun commentaire:
Enregistrer un commentaire