How to Reverse a String without StrReverse Function?

stringReverse without string reverse function.

VbScript has a string function to reverse the string. But, Interviewers think different, they want you to write a program without StrReverse function. How would do that?

Use a word like ‘LeveL’ which is same both ways 🙂 If not, check out this post.

Program:

inputString = InputBox("Enter the string")

For i = Len(inputString) to 1  Step-1
    var= Mid(inputString,  i  , 1)
    reverseString = reverseString & var
Next

Msgbox reverseString

Logic:

We will use a Mid function and for loop to solve this problem. What is a MID function? The Mid Function returns a specified number of characters from a input string. In the image below ‘Testnbug’ is the input string.

Mid(Testnbug, 8, 1) – It returns character ‘g’ because the 8 is the starting point and range is 1. If its (7,1) then it returns 7th character and similarly the other characters. The revString variable is not initiated and hence the first value is ‘Null’.

String reverse without function explanation

Output:

Reverse a String without String Reverse function
Reverse a String without String Reverse function with example

There will be other questions as well like without using Mid function. Replace without the function etc. we will cover them in further posts.

  • Haris

    Hi there! Good article! I’d suggest a small improvement: You can actually do it in half the steps: by going from 1 to the middle of the word, swapping places of the 1st and last letter (n-1), 2nd and (n-2), and so on.

    • TestnBug

      Hi Haris,

      Swapping is making program lengthy.

  • Testudoq

    Use a pen and paper with a mirror?

    Another attempt to skin the schrodinger cat and in good dev approach untested, mildy debugged online! Worked on my machine, honest! Also we know it is not wise to test our own code. 🙂

    I used javascript, however logic of function should be transposable.

    Unsure why I misread vbscript as javascript must have been Freudian. Or never read the requirements. 😉


    r = Inputbox(“Enter the string?)
    Msgbox reversestr;
    function reversestr(e){var r=e.length,s=””;do s+=e.substring(r-1,r),r–;while(r>0);return e+” reversed is “+s+”!”}

    Use a while and count it down from length of string.

    • TestnBug

      Nice one

  • Krishna Pramod

    when can we have another sample program?

    • TestnBug

      In the next posts. very soon.

  • James Crookston

    just found this bit of code and i am trying to change is up a bit for coursework. Can u explain the for loop bit as it gets me a bit lost. thx