• Welcome to Valhalla Legends Archive.
 

Translating Program

Started by Reaper~, November 10, 2004, 05:57 PM

Previous topic - Next topic

Reaper~

Hey I  need help, how would I make a simple program to translate like one letter to another?
Ex. A -> G, B -> W, etc.. etc.. Help is appreciated
Take my advice, I'm not using it.

Yegg

do u mean like a > b > c   and so on?

Dyndrilliac

#2
Quote from: Reaper~ on November 10, 2004, 05:57 PM
Hey I  need help, how would I make a simple program to translate like one letter to another?
Ex. A -> G, B -> W, etc.. etc.. Help is appreciated

Try splitting the string into an array using a null string as your delimiter and then iterate through the array with a For Loop, running a check to see which letter it is and what to replace it with. Here's an example - this is untested:Dim Splt() As String

Public Function Encode(S As String) As String
    Splt = Split(S, vbNullString)
    Dim i As Integer
    For i = 0 To UBound(Splt)
        Select Case LCase$(Splt(i))
            Case "a"
                Splt(i) = Replace(Splt(i), "a", "g")
        End Select
       
        Encode = Encode & Splt(i)
    Next i
End Function
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

TheMinistered

You could make a table of values (an array) from 1-26, and each could have a corresponding value for the proper letter.  i.e. 1st letter is A so in the first element of the table you could have G.

Then you could do something like this:

Private CharTable(1 To 26) As String

Private Sub Test()
Dim lngCounter As Long, strDumpString As String
Dim bytCharacter As Byte

Dim strString As String

strString = "abcde"

CharTable(1) = "G"
CharTable(2) = "s"
CharTable(3) = "A"
CharTable(4) = "x"
CharTable(5) = "I"
'...

For lngCounter = 1 To Len(strString)
    bytCharacter = Asc(Mid$(strString, lngCounter, 1))

    If (bytCharacter >= 65 And bytCharacter <= 90) Then
        strDumpString = strDumpString & CharTable(bytCharacter - 64)
    End If

    If (bytCharacter >= 97 And bytCharacter <= 122) Then
        strDumpString = strDumpString & CharTable(bytCharacter - 96)
    End If
Next lngCounter
End Sub