求助~~ 本人学生 急求~~ !! VB实现字符位移
字符位移就是随机产生十个大写字母,子过程movestr(a(),m,tag as boolean)是把字符数组移动m个位置,当tag为true向左移动,则前m个字符移动到字符数组尾,如abcdefghij左移三个位置后,结果为defghijabc;当tag为false时右移,则后m个字符一道字符数组前,如abcdefghij又移3个字符后,hijabcdefg.其中m是1~10的随机数。如果有高手请尽快给出答案。非常急急~~~万分感谢!!!
[解决办法]
用字符串较简单,给你个思路,将字符串成倍,再取。
Dim s As String, tag As Boolean, m As Integer '产生10个随机大写字母 Dim i As Integer For i = 1 To 10 Randomize s = s & Chr(Int(26 * Rnd + 1) + 64) Next i Randomize m = Int(10 * Rnd + 1) '移位数 Debug.Print s, m s = s & s '字符串长度成2倍 tag = False Debug.Print s, Mid(s, IIf(tag, m + 1, 11 - m), 10) '取移位后的字符串
[解决办法]
function leftshift(s as string) as string leftshift = right(s, len(s) - 1) & left(s, 1)end functionfunction rightshift(s as string) as string leftshift = right(s, 1) & left(s, len(s) - 1)end functionfunction leftshiftn(s as string, n as integer) as string leftshiftn = s for i = 1 to n leftshiftn = leftshift(leftshiftn) nextend functionfunction rightshiftn(s as string, n as integer) as string rightshiftn = s for i = 1 to n rightshiftn = rightshift(rightshiftn) nextend function