So i want to have an application where the user types in a word in a text box and in another text box they put a letter for example "e" and in the third text box they put down what letter they want to replace it with example "f"

So Example:
Eat
E
F

And then the label will show "Fat" So ive tried really hard trying to do it without the replace function because I can't use it. I'm trying to use a loop and a mid function.

Any help will be greatly appreciated!

Member Avatar for gowans07

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.

The solution you propose should work. What code have you already tried?

Member Avatar for gowans07

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
Member Avatar for gowans07

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))).

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

Thanks for the help but since I'm new to visual basic I was having a hard time getting this to work. I understand your loop but I don't know where to put my text boxes...
If I had
txtWord
txtLetter
txtReplace
lblAnswer
cmdNew

where would i put it in the code

i really appreciate the help :)

Member Avatar for gowans07

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.

Member Avatar for gowans07

BitBlt - any suggestions for improving it?

If I had
txtWord, txtLetter, txtReplace, lblAnswer, cmdNew

where would i put it in the code

Enter your code in the cmdNew sub

Private Sub cmdNew_Click()
    Dim intLen As Integer, intPos As Integer, intCounter As Integer
    Dim strNew As String, strBuild As String
    intLen = Len(txtWord)
    lblAnswer.Caption = ""
    intPos = InStr(1, txtWord, txtLetter, vbTextCompare)
    For intCounter = 1 To intLen
        If intCounter <> intPos Then
            strBuild = strBuild & Mid(txtWord, intCounter, 1)
        Else
            strBuild = strBuild & txtReplace.Text
        End If
        
    Next intCounter
    lblAnswer.Caption = strBuild
End Sub

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!

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!

You didn't over complicate it :P I just didn't explain it properly. You are right though, I wanted it to happen on all occurences. Thanks so much for the help!
Merry Christmas! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.