Results 1 to 8 of 8

Thread: [RESOLVED] Replace letters in string

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Resolved [RESOLVED] Replace letters in string

    Hello everybody!

    Beginers question:
    I do not know how to make a code where VB6 would automaticaly change the entered letters.

    Example:
    I have my own special alphabet letters: A=KU, B=DE, C=VE, D=SI
    I enter a word into textbox: ABCDE
    <then I press the button TRANSFORM>
    I get out the result: KUDEVESI

    Can you help me?

    regards,
    Dejan

  2. #2
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: Replace letters in string

    This was a multi pc language written for a friend. It shows how to add custom alphabets to your application in real-time by switching them in the 'txtOutput' when you type them in the 'txtInput'. Hope it helps

    vb Code:
    1. Private Sub txtInput_Change()
    2. txtOutput.Text = Ascii_CustomAlphabet(txtInput.Text, " 97 ", " 65 ", " 98 ", " 66 ", " 99 ", " 67 ", " 100 ", " 68 ", " 101 ", " 69 ", " 102 ", " 70 ", " 103 ", " 71 ", " 104 ", " 72 ", " 105 ", " 73 ", " 106 ", " 74 ", " 107 ", " 75 ", " 108 ", " 76 ", " 109 ", " 77 ", " 110 ", " 78 ", " 111 ", " 79 ", " 112 ", " 80 ", " 113 ", " 81 ", " 114 ", " 82 ", " 115 ", " 83 ", " 116 ", " 84 ", " 117 ", " 85 ", " 118 ", " 86 ", " 119 ", " 87 ", " 120 ", " 88 ", " 121 ", " 89 ", " 122 ", " 90 ")
    3. End Sub
    4.  
    5. Function Ascii_CustomAlphabet(Text As String, LcaseA As String, UcaseA As String, LcaseB As String, UcaseB As String, LcaseC As String, UcaseC As String, LcaseD As String, UcaseD As String, LcaseE As String, UcaseE As String, LcaseF As String, UcaseF As String, LcaseG As String, UcaseG As String, LcaseH As String, UcaseH As String, LcaseI As String, UcaseI As String, LcaseJ As String, UcaseJ As String, LcaseK As String, UcaseK As String, LcaseL As String, UcaseL As String, LcaseM As String, UcaseM As String, LcaseN As String, UcaseN As String, LcaseO As String, UcaseO As String, LcaseP As String, UcaseP As String, LcaseQ As String, UcaseQ As String, LcaseR As String, UcaseR As String, LcaseS As String, UcaseS As String, LcaseT As String, UcaseT As String, LcaseU As String, UcaseU As String, LcaseV As String, UcaseV As String, LcaseW As String, UcaseW As String, LcaseX As String, UcaseX As String, LcaseY As String, UcaseY As String, LcaseZ As String, UcaseZ As String) As String
    6.  
    7. Dim NextKey As String, InputText As String, TextLength As Integer, NextKey2 As String, NewSent As String, SpaceBar As String, Spacing As Integer, SendSpacing As Integer
    8. Let InputText$ = Text$
    9. Let TextLength% = Len(InputText$)
    10. Do While Spacing% <= TextLength%
    11. Let SpaceBar = " 32 "
    12. Let Spacing% = Spacing% + 1
    13. Let NextKey$ = Mid$(InputText$, Spacing%, 1)
    14. Let NextKey2$ = Mid$(InputText$, Spacing%, 2)
    15. If NextKey$ = "A" Then Let NextKey$ = UcaseA$
    16. If NextKey$ = "a" Then Let NextKey$ = LcaseA$
    17. If NextKey$ = "B" Then Let NextKey$ = UcaseB$
    18. If NextKey$ = "b" Then Let NextKey$ = LcaseB$
    19. If NextKey$ = "C" Then Let NextKey$ = UcaseC$
    20. If NextKey$ = "c" Then Let NextKey$ = LcaseC$
    21. If NextKey$ = "D" Then Let NextKey$ = UcaseD$
    22. If NextKey$ = "d" Then Let NextKey$ = LcaseD$
    23. If NextKey$ = "E" Then Let NextKey$ = UcaseE$
    24. If NextKey$ = "e" Then Let NextKey$ = LcaseE$
    25. If NextKey$ = "F" Then Let NextKey$ = UcaseF$
    26. If NextKey$ = "f" Then Let NextKey$ = LcaseF$
    27. If NextKey$ = "G" Then Let NextKey$ = UcaseG$
    28. If NextKey$ = "g" Then Let NextKey$ = LcaseG$
    29. If NextKey$ = "H" Then Let NextKey$ = UcaseH$
    30. If NextKey$ = "h" Then Let NextKey$ = LcaseH$
    31. If NextKey$ = "I" Then Let NextKey$ = UcaseI$
    32. If NextKey$ = "i" Then Let NextKey$ = LcaseI$
    33. If NextKey$ = "J" Then Let NextKey$ = UcaseJ$
    34. If NextKey$ = "j" Then Let NextKey$ = LcaseJ$
    35. If NextKey$ = "K" Then Let NextKey$ = UcaseK$
    36. If NextKey$ = "k" Then Let NextKey$ = LcaseK$
    37. If NextKey$ = "L" Then Let NextKey$ = UcaseL$
    38. If NextKey$ = "l" Then Let NextKey$ = LcaseL$
    39. If NextKey$ = "M" Then Let NextKey$ = UcaseM$
    40. If NextKey$ = "m" Then Let NextKey$ = LcaseM$
    41. If NextKey$ = "N" Then Let NextKey$ = UcaseN$
    42. If NextKey$ = "n" Then Let NextKey$ = LcaseN$
    43. If NextKey$ = "O" Then Let NextKey$ = UcaseO$
    44. If NextKey$ = "o" Then Let NextKey$ = LcaseO$
    45. If NextKey$ = "P" Then Let NextKey$ = UcaseP$
    46. If NextKey$ = "p" Then Let NextKey$ = LcaseP$
    47. If NextKey$ = "Q" Then Let NextKey$ = UcaseQ$
    48. If NextKey$ = "q" Then Let NextKey$ = LcaseQ$
    49. If NextKey$ = "R" Then Let NextKey$ = UcaseR$
    50. If NextKey$ = "r" Then Let NextKey$ = LcaseR$
    51. If NextKey$ = "S" Then Let NextKey$ = UcaseS$
    52. If NextKey$ = "s" Then Let NextKey$ = LcaseS$
    53. If NextKey$ = "T" Then Let NextKey$ = UcaseT$
    54. If NextKey$ = "t" Then Let NextKey$ = LcaseT$
    55. If NextKey$ = "U" Then Let NextKey$ = UcaseU$
    56. If NextKey$ = "u" Then Let NextKey$ = LcaseU$
    57. If NextKey$ = "V" Then Let NextKey$ = UcaseV$
    58. If NextKey$ = "v" Then Let NextKey$ = LcaseV$
    59. If NextKey$ = "W" Then Let NextKey$ = UcaseW$
    60. If NextKey$ = "w" Then Let NextKey$ = LcaseW$
    61. If NextKey$ = "X" Then Let NextKey$ = UcaseX$
    62. If NextKey$ = "x" Then Let NextKey$ = LcaseX$
    63. If NextKey$ = "Y" Then Let NextKey$ = UcaseY$
    64. If NextKey$ = "y" Then Let NextKey$ = LcaseY$
    65. If NextKey$ = "Z" Then Let NextKey$ = UcaseZ$
    66. If NextKey$ = "z" Then Let NextKey$ = LcaseZ$
    67. If NextKey$ = " " Then Let NextKey$ = SpaceBar
    68. Let NewSent$ = NewSent$ + NextKey$
    69. If SendSpacing% > 0 Then Let SendSpacing% = SendSpacing% - 1
    70. Loop
    71. Ascii_CustomAlphabet = NewSent$
    72. End Function
    Last edited by Neato; Nov 19th, 2007 at 09:11 PM.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Replace letters in string

    dgorenc, the problem is complicated by the fact that, for example, your translation of B gives a D which is translated to something else. The following handles that.

    Code:
        Const PLAIN_TEXT = "ABCD"
        Dim strCrypt As String
        Dim strText() As String
        Dim lngIndex As Long
        
        ' Split the text into it's individual letters
        ReDim strText(0)
        For lngIndex = 1 To Len(PLAIN_TEXT)
            strText(lngIndex - 1) = Mid$(PLAIN_TEXT, lngIndex, 1)
            ReDim Preserve strText(UBound(strText) + 1)
        Next
        ' Remove the last entry because it's blank
        ReDim Preserve strText(UBound(strText) - 1)
        
        ' Translate the individual letters
        For lngIndex = 0 To UBound(strText)
            Select Case strText(lngIndex)
                Case "A"
                    strText(lngIndex) = "KU"
                Case "B"
                    strText(lngIndex) = "DE"
                Case "C"
                    strText(lngIndex) = "VE"
                Case "D"
                    strText(lngIndex) = "SI"
                Case "E" ' etc
            End Select
        Next
        
        ' Put the string back together
        For lngIndex = 0 To UBound(strText)
            strCrypt = strCrypt & strText(lngIndex)
        Next
    
        MsgBox strCrypt

  4. #4

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Replace letters in string

    Hope this will help.
    Code:
    Option Explicit
    
    Dim myAlphabet() As String
    Dim InitDone As Boolean
    
    Sub InitAlphabet()
       '-- make sure sCode contains 26 items separated by comma
       Const sCode = "SN,PH,HU,AT,VS,BK,WU,JZ,WB,YJ,NT,BP,MH,QQ,GH,VV,PZ,XF,SZ,GN,CZ,RA,OC,CU,HB,HJ"
       myAlphabet = Split(sCode, ",")
       InitDone = True
    End Sub
    
    Function Encrypt(sInText As String) As String
       Dim i As Integer
       Dim sOutText As String
       Dim Ch As String
       
       If InitDone = False Then InitAlphabet
       
       For i = 1 To Len(sInText)
          Ch = UCase(Mid(sInText, i, 1))
          If Ch >= "A" And Ch <= "Z" Then
             Ch = myAlphabet(Asc(Ch) - 65) '-- Asc("A") = 65
          Else
             Ch = "--"
          End If
          sOutText = sOutText & Ch
       Next
       Encrypt = sOutText
    End Function
    
    Sub CommandButton1_Click()
       MsgBox Encrypt(Text1.Text)
    End Sub
    
    Sub Test()
       MsgBox Encrypt("Bill Gates")
       MsgBox Encrypt("HELLO")
    End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Re: Replace letters in string

    I am impressed with the responce and the quality of it.
    PROBLEM SOLVED IN 5 MIN! for me

    Thank you all!

    Dejan

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Replace letters in string

    Now that we've helped you, you can help us by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button which will let everyone know that you have your answer.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Re: [RESOLVED] Replace letters in string

    done.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width