Reverse a string without using reversed() or [::-1]?

前端 未结 30 2596
南旧
南旧 2020-11-30 19:44

I came across a strange Codecademy exercise that required a function that would take a string as input and return it in reverse order. The only problem was you could not use

30条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 20:09

    The way I can think of without using any built-in functions:

    a = 'word'
    count = 0
    for letter in a:
        count += 1
    
    b = ''
    for letter in a:
        b += a[count-1]
        count -= 1
    

    And if you print b:

    print b
    drow
    

提交回复
热议问题