0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

スタックを使って文字列を逆順にする

Last updated at Posted at 2018-08-19

前書き

これはある学生の日々の学習の成果を記した備忘録であります
実行環境

  • macOS 10.12.6
  • Python 3.7.0

やったこと

"Hello"という文字列を"olleH"に変換する
以下実行したコード

str = "Hello"
print(str)
list = []

for c in str:
    list.append(c)

reverse = ""
while len(list):
     reverse += list.pop()

print(reverse)

結果

% python3 reverse.py                                                                                                                                                                                        [16:55:28]
Hello
olleH

追記

関数にして引数で文字列を受け取るver

def reverse(str): 
    list = []

    for c in str:
        list.append(c)

    reverse = ""
    while len(list):
        reverse += list.pop()

    print("output: "+reverse)

reverse(input("imput:  "))

実行結果

% python3 reverse.py                                                                                                                                                                                       
imput:  hello
output: olleh
0
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?