How To Implement Replace Function Without Using The Inbuilt String Function

This Post Will Cover

Share and Enjoy !

A question was asked to me by one of the reputed firms that can you replace a few characters by another set of characters without using the inbuilt string function…..

Oh… this was a very popular topic during the last few days …finally I found the solution…
I am sharing this function below written…

It is changing HYD with CYB…

Sub Macro1()

Dim bText() As Byte
Dim j As Integer
j = 0
Dim k As Integer
k = 0

Dim finalq As String
finalq = ""
'Assign the sting with null values
tttt = Len(inputq) + 3
ReDim a(tttt)
Dim MyString As String: MyString = inputq
'Check the string one by one..character by character
bText = MyString
For i = 0 To UBound(bText) - 1
If (bText(i) = 0) Then
Else
a(j) = Chr(bText(i))
j = j + 1
End If
Next
Do
If a(k) = "" Then
Else
'Checking Upper case and Lower case
If ((a(k) = "H" Or a(k) = "h") And (a(k + 1) = "Y" Or a(k + 1) = "y") And (a(k + 2) = "D" Or a(k + 2) = "d")) Then
If a(k) = "H" Then
a(k) = "C"
Else
a(k) = "c"
End If
If (a(k + 1) = "Y") Then
a(k + 1) = "Y"
Else
a(k + 1) = "y"
End If
If (a(k + 2) = "D") Then
a(k + 2) = "B"
Else
a(k + 2) = "b"
End If
finalq = finalq & a(k) & a(k + 1) & a(k + 2)
k = k + 2
Else
finalq = finalq & a(k)
End If
End If
k = k + 1
Loop Until (k = UBound(a))
MsgBox finalq
inputx = InputBox("Want to do again")
If inputx = "Y" Then
Call Macro1
Else
End If
End Sub

We are done. This can be more dynamic by using replacing character is taken from the user.

2 thoughts on “How To Implement Replace Function Without Using The Inbuilt String Function”

1. Anonymous says:

hello,
the program actually runs very well but it actually wastes 2 bytes space. Inplace u can write as:
For i = 0 To UBound(bText) – 3

2. Animesh Chatterjee says:

Hi,
Thank you very much for the upgraded solution …Yes, i agree that the array deceleration is not up to the mark. It was a quick solution given.
I will write a fresh code without the waste of space.

Thanks for the review and happy reading.