1,105,331 Community Members

How to replace specific letter from string without replace function

Member Avatar
kiki256
Newbie Poster
4 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
gowans07
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
BitBlt
Practically a Posting Shark
895 posts since Feb 2011
Reputation Points: 442 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

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

Member Avatar
gowans07
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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
gowans07
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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))).

Member Avatar
kiki256
Newbie Poster
4 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
gowans07
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
gowans07
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

BitBlt - any suggestions for improving it?

Member Avatar
hkdani
Posting Pro in Training
433 posts since Nov 2007
Reputation Points: 39 [?]
Q&As Helped to Solve: 46 [?]
Skill Endorsements: 2 [?]
 
0
 

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
Member Avatar
BitBlt
Practically a Posting Shark
895 posts since Feb 2011
Reputation Points: 442 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 16 [?]
Featured
 
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!

Member Avatar
kiki256
Newbie Poster
4 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!

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! :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: