Thread: Visual Basic?
View Single Post
  #3   Spotlight this post!  
Unread 20-04-2010, 20:02
EHaskins EHaskins is offline
Needs to change his user title.
AKA: Eric Haskins
no team (CARD #6 (SCOE))
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Elkhorn, WI USA
Posts: 998
EHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond repute
Send a message via MSN to EHaskins
Re: Visual Basic?

Here is the corrected code implemented in a console application.

Look at the comments for corrections.

Code:
Imports System.Text

Module Module1
    Sub Main()
        Do
            Dim input As String = Console.ReadLine
            If String.IsNullOrEmpty(input) Then
                Exit Sub
            End If
            Dim strOutput As String = GetPigLatin(input)
            Console.WriteLine(String.Format("The Pig Latin Translation is: {0}", strOutput))
        Loop
    End Sub

    Private Function GetPigLatin(ByVal input As String) As String
        Dim words As String() = input.Split(" ")
        Dim output As New StringBuilder() 'StringBuilder is much faster than regular string concatenation. 

        For Each word In words
            output.Append(GetPigLatinWord(word))
            output.Append(" ")
        Next
        Return output.ToString() 'Get the final string from the StringBuilder
    End Function

    Private Function GetPigLatinWord(ByVal strInput As String) As String
        Dim str1stCharacter As String = ""
        Dim strOutput As String = ""
        Dim intStringLength As Integer = 0
        str1stCharacter = Microsoft.VisualBasic.Left(strInput, 1)
        Select Case str1stCharacter.ToUpper()
            Case "A", "E", "I", "O", "U"
                strOutput = strInput & "WAY"
            Case Else
                'THIS IS WHAT I CHANGED. You were using fixed lengths to capture from each end of the word. 
                'That meant that only 5 char words would work with your implementation.
                strOutput = strInput.Substring(1) & strInput.Substring(0, 1) & "AY"
        End Select
        Return strOutput.ToLower 'makes output prettier
    End Function
End Module
__________________
Eric Haskins KC9JVH
Reply With Quote