Wednesday, April 28, 2010

Write a Script in VB to reverse a string given by the user

This VB Script demonstrates how we can reverse a string or a number given by the user and print it in a reverse order. Here we use Len and Mid methods of VB Script library. In the method one we will find the length of the string and store in y variable and then use a for loop in a descending order and give step as -1.



Method 1

MyStr = InputBox(“Enter a string to be reversed”)
y = Len(MyStr)
For x = y To 1 Step -1
aaachar = Mid(MyStr,x,1)
aaaNewStr = NewStr & char
Next
aaamsgbox newstr


Method 2 using the StrReverse Method.

val=Inputbox("Please enter any number or a string")
a = strReverse(val)
msgbox "The reverse of "&val& " is: "&a

No comments:

Post a Comment