VB脚本中的字符串反转,不使用内置函数

时间:2014-05-27 15:11:11

标签: string vbscript

 option explicit

dim r, res, num

num= cint(inputbox("Enter the number"))

  do while(num > 0)

 r= num mod 10

 num= num\10

res= res & r

loop

msgbox res

这是代码,现在我的问题是这对输入1234完全正常,如果输入是0123它只是打印321这是错误的。它需要打印3210。 我无法弄清楚,尝试了很多但是徒劳,任何帮助都会受到赞赏 谢谢和问候

4 个答案:

答案 0 :(得分:1)

您必须决定是要撤消字符串还是数字(意外地表示为小数)。如果你想反转字符串,你应该

  1. 不将(字符串)输入转换为数字/整数
  2. 使用字符串操作:Mid()进行读取,连接和&用于建设
  3. 已添加:在(演示/非制作)代码中

    Option Explicit
    
    Function rev(s)
      Dim p
      For p = Len(s) To 1 Step -1
          rev = rev & Mid(s, p, 1)
      Next
    End Function
    
    Dim s
    For Each s In Array("1234", "0123")
        WScript.Echo s, rev(s)
    Next
    

    输出:

    1234 4321
    0123 3210
    

答案 1 :(得分:0)

试试这个:

Dim   num,   rev
num   =   inputbox("Enter a number")
If   Len(num)=4 Then
rev = rev*10 + num mod 10
num = num/10
num = left(num,3)
rev = rev*10 + num mod 10
num = num/10
num = left(num,2)
rev = rev*10 + num mod 10
num = num/10
num = left(num,1)
rev = rev*10 + num mod 10
msgbox "Reverse Order of the number is "&rev 
Else
msgbox "Number, you entered is not a 4 digit number"
End If

答案 2 :(得分:0)

字符串反向程序,不使用反向字符串函数&中功能。

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class User(Base):
    __tablename__ = 'user'
    __table_args__ = {'schema' : self.s}
    def __init__(self, s):
        self.s = s

u = User('customschema')

答案 3 :(得分:0)

str =输入框("输入数字")

转=""

设置regx = New RegExp

regx.Global = True

regx.IgnoreCase = True

regx.Pattern ="。{1}"

设置colchars = regx.Execute(str)

对于i = 0到colchars.Count-1

rev = colchars.Item(i)& rev

下一步

MsgBox rev