問題1.2

世界で闘うプログラミング力を鍛える150問

引き続き〜☆

問題

CかC++で、void reverse(char* str)という'\0'終端の文字列を逆に並び替える関数を実装してください。


CかC++って指定されてるけど、相変わらず面倒なのでPythonで行きます。

#encoding: utf-8


def reverse(str):
    '''
    引数の文字列を逆転する関数
    >>> reverse('hello')
    'olleh'
    >>> reverse('')
    ''
    >>> reverse('h')
    'h'
    '''
    # 逆転させた文字列を代入させるリスト
    reversed_str = []

    # 文字列の逆転
    counter = len(str)
    while counter >= 0:
        reversed_str.append(str[counter:counter + 1])
        counter = counter - 1

    # 逆転させた文字のリストを文字列に変換してリターン
    return ''.join(reversed_str)


def _test():
    import doctest
    doctest.testmod()


if __name__ == '__main__':
    _test()


この辺はサクサクいきましょう。