Welcome to automated-360.com

Recently one of my colleagues put an interesting situation to script in vbs. It was to reverse a string without using Mid or string Reverse function. For example, if the given string is – AUTOMATED 360 , the result should be DETAMOTUA 063.

Since most of us are likely to use the built in functions and not to think about other ways of doing the same, it sounds like a bit tough. I also had the same situation, but a few minutes of analysis resulted me the solution for the same. The trick is to put all the characters in array and then print the array in reverse order.

below is the code using array to reverse the string.

msgbox ReverseString("AUTOMATED 360")

'Function Name:  ReverseString
'Description: Main Function to call for reverse the string without using String Reverse or mid functions.
'Input: wString(String)
'Output: ReverseString(String)
'Author: Saket Kumar [Automated-360.com]
Function ReverseString(wString)
 ' Split the words in string to put in array
   wStringArr = Split(wString, " ")
   For nCnt = 0 to UBOUND(wStringArr)
             'Put all the letters of word in array
             MyArr = singleCharArr(wStringArr(nCnt))
            'Print array in reverse order
             for n = UBOUND(MyArr) to 0 step -1
                      str = str & MyArr(n)
             next
             str = str & " "
    Next
    ReverseString = str
End Function

'Function Name:  singleCharArr
'Description: Function to put all letters of a string into an array
'Input: inputString(String)
'Output: singleCharArr(array)
'Author: Saket Kumar [Automated-360.com]
Function singleCharArr(inputString)
    'Check if input is blank
    If inputString <> "" then
          Dim oRegEx, colChars
           Set oRegEx = New RegExp
           oRegEx.Global = True
           oRegEx.IgnoreCase = True
           oRegEx.Pattern = ".{1}"
           Set colChars = oRegEx.Execute(inputString)
           Dim arrChars()
           For i = 0 to colChars.Count - 1
                   Redim Preserve arrChars(i)
                   arrChars(i) = colChars.Item(i)
           Next
           Set colChars = Nothing
          Set oRegEx = Nothing
          singleCharArr = arrChars
          Erase arrChars
     End If
End Function

Here is one more nice way to do this using array again.

wString = "AUTOMATED 360"
arrPar = Split(wString, " ")
arrParRev = Split(wString, " ")
For n = 0 to UBOUND(arrPar)
	For i = 32 to 122
		if not i = 35 then
			arrPar(n) = replace(arrPar(n), Chr(i), Chr(i) & "#")
		end if
	Next
	arr = Split(arrPar(n), "#")
	arrRev = Split(arrPar(n), "#")
	For j = UBOUND(arr) to 0 step -1
		arrRev(UBOUND(arr)-j) = arr(j)
	Next
	arrParRev(n) = join(arrRev, "")
Next
y = join(arrParRev, " ")
msgbox y
See also  Error Handling in VBScript

5 COMMENTS

  1. Hello Saket,

    Nice code for reverse string, I have faced this question in most of interviews.

    Can I have function for generating the qtp results with screen shots in a PDF or word formate please?

    Thanks in advance.

    REgards,

    Sreekanth

  2. Sub abc()

    srt = “Automation is good”
    Count = Split(srt, ” “)
    ‘MsgBox UBound(Count)
    For a = 0 To UBound(Count)
    For i = 1 To Len(Count(a))
    temp1 = temp1 & Left(Right(Count(a), i), 1)

    Next

    temp2 = temp2 & ” ” & temp1

    temp1 = “”
    Next

    MsgBox temp2
    End Sub

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.