0

I have a vb.net project on Soundex. For those who don't know, Soundex takes your inputted word and exports it to a letter and numbers.

For example:

Word: Carrot

Output: C663

I'm tasked with removing the second digit if they're directly next to each other and are the same number. So for this example, C663 would just need to output as C63. I can't seem to figure it out. I was attempting to use the Distinct clause but I just can't figure it out.

Here's my code, any help would be greatly appreciated.

Public Class Form1

Dim Word As String

Private Sub btnEncode_Click(sender As Object, e As EventArgs) Handles btnEncode.Click

    Word = txtInput.Text
    txtOutput.Text = Output(Word)

End Sub

Public Function Output(Word As String) As String

    Return Output(Word, 4)

End Function

Public Function Output(Word As String, Length As Integer) As String

    Dim returnValue As String = ""

    Dim Size As Integer = Word.Length

    If (Size > 1) Then

        Word = Word.ToUpper()

        Dim Chars() As Char = Word.ToCharArray()
        Dim i As Integer
        Dim wordSize As Integer = Size - 1
        Dim value As Integer
        Dim newString As New System.Text.StringBuilder

        newString.Append(Chars(0))

        For i = 1 To wordSize
            Select Case Chars(i)
                Case "A", "E", "I", "O", "U", "H", "W", "Y"
                    value = 0
                Case "B", "F", "P", "V"
                    value = 1
                Case "C", "G", "J", "K", "Q", "S", "X", "Z"
                    value = 2
                Case "D", "T"
                    value = 3
                Case "L"
                    value = 4
                Case "M", "N"
                    value = 5
                Case "R"
                    value = 6
            End Select

            If value <> 0 Then
                newString.Append(value)
            End If

        Next

    End If

    Return returnValue
End Function
End Class
braX
  • 11,506
  • 5
  • 20
  • 33
  • There are several techniques that could be used to manage this; the one that I would use would be to save the value previously found, and if the new value matches the previous one, don't actually append it to the string. – Jeff Zeitlin Mar 21 '18 at 19:42
  • You need an if statement to test the preceeding character. You also dont need a stringbuilder since Soundex codes are only 4 characters. Please take the [tour] and read [ask] – Ňɏssa Pøngjǣrdenlarp Mar 21 '18 at 19:42

2 Answers2

0

Use a loop to check each character to see if they are next to each other and the same, if so dont add the character to the new string:

Public Function Output(Word As String, Length As Integer) As String

Dim returnValue As String = ""

Dim Size As Integer = Word.Length
'initialize temp string to first character of word
Dim newcondensedWord AS String = Word(0)
'Loop starting at index 1 or second character compare each character to previous during loop, if the same continue else append to variable newcondensedWord
FOR i AS Integer = 1 TO Word.Length - 1
     Dim temp as CHAR = Word(i - 1)
     IF Word(i) = Word(i - 1) THEN
          Continue
     ELSE
          newcondensedWord += Word(i)
     END IF
NEXT

'Now that we have removed the characters that are duplicates and adjacents set word equal to the condensed word
Word = newcondensedWord
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
0

Just check for the character in your current loop. Here's a working function demonstrating what you need to do.

Public Function Output(Word As String, Length As Integer) As String

    Dim returnValue As String = String.Empty

    Dim Size As Integer = Word.Length

    If (Size > 1) Then

        Word = Word.ToUpper()

        Dim Chars() As Char = Word.ToUpper().ToCharArray()
        Dim value As Char
        Dim newString As New System.Text.StringBuilder

        newString.Append(Chars(0))

        For i = 1 To Size - 1
            Select Case Chars(i)
                Case "A", "E", "I", "O", "U", "H", "W", "Y"
                    value = "0"c
                Case "B", "F", "P", "V"
                    value = "1"c
                Case "C", "G", "J", "K", "Q", "S", "X", "Z"
                    value = "2"c
                Case "D", "T"
                    value = "3"c
                Case "L"
                    value = "4"c
                Case "M", "N"
                    value = "5"c
                Case "R"
                    value = "6"c
            End Select

            If value <> "0" Then
                If newString.Length > 0 AndAlso newString(newString.Length - 1) = value Then
                    Continue For
                End If
                newString.Append(value)
            End If

        Next

        returnValue = newString.ToString()

    End If

    Return returnValue
End Function
Ryan Gunn
  • 1,161
  • 7
  • 18
  • Thank you so much for the reply, it's greatly appreciated. I'm a pretty brand new programmer. Can I ask you, does this sort of logic just come with time or is it a natural ability? VB.NET is my first language and I'm only about 2 months in. Every time I see the solution I understand it perfectly, I just can't seem to get it right without asking. Thank you very much for the answer! –  Mar 21 '18 at 20:13