反转字符串,但其指定的字串不反转nim
2019-10-20 本文已影响0人
beasu
反转字符串,但其指定的字串不反转
import stacks
proc revstr(src,token:string):string =
var
s = Stack[char]()
i = 0
sl = src.len
tl = token.len
while i < sl :
if i <= sl - tl:
if src[i..<(i+tl)] == token:
for ti in countdown(tl - 1,0 ):
s.push(token[ti])
i += tl
else:
s.push(src[i])
i.inc
else:
s.push(src[i])
i.inc
while not s.empty:
result.add(s.pop)
return result
const
src = "Welcome you, my friend you"
token = "you"
echo revstr(src,token)