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’.
Output:
There will be other questions as well like without using Mid function. Replace without the function etc. we will cover them in further posts.