Monday, March 14, 2011

VBScript Program to reverse a string without using strreverse

'Program to reverse a string without using strreverse

MyStr = inputbox("Enter the String:")
nCnt = Len(MyStr)

For i = 1 to nCnt

   RevStr = Mid(MyStr,i,1)&RevStr

Next

Msgbox RevStr

Click here for more VBScript codes.

22 comments:

  1. Replies

    1. Dim str, revstr, strlen
      Str = "TestingQ"
      strlen = Len(Str)
      For i = strlen To 1 Step -1
      revstr = revstr + mid (Str,i,1)
      Next
      print revstr
      'Msgbox revstr
      If instr(revstr, "QgnitseT") Then
      Reporter.ReportEvent micPass, "verify rev str", "reverse string pass"
      print "reverrse done is" & true
      Else
      Reporter.ReportEvent micFail, "verify rev str", "reverse string fail"
      print "reverse done is " & false
      End If

      Delete
    2. What is use of
      print "reverse done is " & false or
      print "reverse done is " & true ?

      Delete
  2. This is wrong.
    reverse the loop
    For nCnt to 1 Step -1

    ReplyDelete
    Replies
    1. @ Chandrasekar : We need to repeat the reversing for every character. Where as in your logic, you are excluding one of the characters if you are trying from 1st character till last-1 (you are missing the last character). Looks like your logic needs a correction. BTW I already executed the above code and it works fine. Please let me know if you need any more information.

      Delete
    2. can u please elaborate the logic

      Delete
  3. Replies
    1. I dont understand.. could you please elaborate?

      Delete
  4. Try this:

    dim a,b
    str = inputbox("enter the string:")

    For n = len(str) to 1 step- 1
    b=Mid(str,n,1)
    a = a+b

    Next
    Msgbox ("the string is"&a)

    ReplyDelete
    Replies
    1. I too did the same at home, the ans is as follows:


      Dim str, revstr, strlen
      Str = "TestingQ"
      strlen = Len(Str)
      For i = strlen To 1 Step -1
      revstr = revstr + mid (Str,i,1)
      Next
      print revstr
      'Msgbox revstr
      If instr(revstr, "QgnitseT") Then
      Reporter.ReportEvent micPass, "verify rev str", "reverse string pass"
      print "reverrse done is" & true
      Else
      Reporter.ReportEvent micFail, "verify rev str", "reverse string fail"
      print "reverse done is " & false
      End If

      Delete
  5. hi,

    Can any one correct my code if it need to as am not getting required result

    am trying to reverse a string

    Dim x
    x=inputbox("Enter a string")
    cnt=len(x)
    Do until cnt=0
    y = mid(x,cnt,1)&y
    cnt=cnt-1
    Loop
    msgbox y

    ReplyDelete
    Replies
    1. Hi jayasree,

      please tell me..........

      what is "cnt"

      Delete
    2. cnt is a variable
      which is assigned Zero

      Delete
  6. @ jayasree - this will work :
    Dim x
    x=inputbox("Enter a string")
    cnt=len(x)
    Do until cnt=0
    y = y&mid(x,cnt,1)
    cnt=cnt-1
    Loop
    msgbox y

    ReplyDelete
  7. Any know, How to open a windows based file. But should not using systemutil.run and invokeapplication. Any alternative way is their.

    ReplyDelete
  8. option explicit
    dim i,str1,str,slen
    str = "string"
    i = 1
    slen = len(str)
    for i = slen to 1 step -1
    str1 = str1&mid(str,i,1)
    next
    msgbox str1

    ReplyDelete
  9. I appreciate Nrusingha Acharya
    you put it in a simple way.

    ReplyDelete
  10. Please find my approach below.

    Dim myStr, revStr, nCnt, nVar
    myStr = "Test"
    nCnt = Len(myStr)

    For i=nCnt To 1 Step-1

    nVar = Mid(myStr, i, 1)

    revStr = revStr & nVar

    Next

    print revStr

    ReplyDelete

Please leave your comment here...