Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » » How To Implement Replace Function Without Using The Inbuilt String Function

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

Oh... this was a very popular topic during 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
inputq = InputBox("Enter your string")
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 if taken from user.

«
Next
Newer Post
»
Previous
Older Post

2 comments:

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

    ReplyDelete
  2. 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.

    ReplyDelete