Hi there, i would use the Len function to retrive the length of the word entered. Then do a loop from 1 to the length and retrieve the Asci value of each single letter, then get the Asci value from the letter the user wants to change. Then if those two values match then a replacement should take place. Hope this helps.
gowans07
Junior Poster in Training
51 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
The solution you propose should work. What code have you already tried?
BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14
Okay briefly tested this should work for you
Private Sub Command1_Click()
LenWord = Len(TxtWord)
AscLet = Asc(TxtLetter)
For i = 1 To LenWord
'MsgBox Asc(Mid(TxtWord, i, LenWord))
If Asc(Mid(TxtWord, i, LenWord)) = AscLet Then
TxtOutput = TxtOutput & TxtSwap
Else
TxtOutput = TxtOutput & Chr(Asc(Mid(TxtWord, i, LenWord)))
End If
Next i
End Sub
gowans07
Junior Poster in Training
51 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Few ammendments if you wanted them, you can make the first letter always upper case by doing something like this if i = 1 then UCASE(Asc(Mid(TxtWord, i, LenWord))).
gowans07
Junior Poster in Training
51 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
There's two different views within working with vb6 first is the code view this is where you physically write in the code you wish to be ran. next is the design/object view. In this view you can draw out objects such as text boxes, labels etc... You will need to draw a command buttoon and some text boxes then give them appropriate names such as TxtWord etc.
gowans07
Junior Poster in Training
51 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
BitBlt - any suggestions for improving it?
gowans07
Junior Poster in Training
51 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Fun little exercise. I did notice some interesting things:
1. The OP says it should replace one letter with one other letter. I assume he meant "replace ALL OCCURRENCES or one letter with a different letter".
2. What about if you want to replace the letter with a string of letters? Like "Eat", replace "E" with "Wh" to get "What".
3. What about if you want to replace a string of letters with a single letter or a different string? Like "Eat", replace "at" with "el" to get "Eel".
Anyway, I probably made it way more complicated than the OP originally intended, but here's what I came up with:
Private Sub cmdNew_Click()
Dim intPos As Integer
Dim lastIntPos As Integer
' Initialize
lastIntPos = 1
Me.lblAnswer.Caption = ""
' Now iterate, looking for txtLetter value inside txtWord...
Do
intPos = InStr(lastIntPos, Me.txtWord.Text, Me.txtLetter.Text, vbTextCompare)
If intPos = 0 Then ' zero means "I didn't find it"
Exit Do
End If
' assemble the caption with the starting caption from the last iteration,
' add all the letters in-between last iteration that AREN'T getting replaced,
' then tack on the string from txtReplace
Me.lblAnswer.Caption = Me.lblAnswer.Caption & Mid(Me.txtWord.Text, lastIntPos, intPos - lastIntPos) & Me.txtReplace.Text
' Prepare for the next iteration
lastIntPos = intPos + 1
Loop
' Now pick up any leftover characters at the end of txtWord that haven't been replaced.
Me.lblAnswer.Caption = Me.lblAnswer.Caption & Mid(Me.txtWord.Text, lastIntPos + Len(Me.txtLetter.Text) - 1)
' And, we're done!
End Sub
I tested this with all the scenarios (including where the "replace" letter was not in the string at all) and it seems to work fine. And, just for your interest and edification, I included comments to help you figure out what I was doing.
Hope you enjoy it! Merry Christmas to all!
BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14