2020年9月29日 星期二

2020.09.29 by JK C++ reverse string , min memory

 

#include <iostream>

#include <string>

using namespace std;

//2020.09.29 by JK  C++  reverse string , min memory , 1  char pointer +  switchtimes = 2xL


int main()

{

    

    string A_str = "12345678";

    int L = A_str.length();

    std::cout   <<A_str <<'\n';


    char* temp=  new char  ;

    int j = 0;

    

    

    int startL = 0;

    int stopL = startL+ L/2;


    for (int i = startL; i < stopL; i++)

    {


        *temp = A_str[0 + i];

        //std::cout << *temp;

        //std::cout << A_str[0 + i];

        //std::cout << A_str[L - i];

        A_str[0 + i] = A_str[L - 1 - i];

        //std::cout << *temp;

        A_str[L - 1 - i] = *temp;

 

    }

    std::cout << j << '\n' << A_str << '\n';

}





#################################

#Python


def str_Reverse(input_str):
    L=int(input_str.__len__())
    start=0
    end=start+ int(L/2)
    temp=0
    for i in range(start , end):
        temp=input_str[i]
        input_str[i]=input_str[L-i-1]
        input_str[L-i-1] =temp


    return input_str


if __name__ == '__main__':

    input_str=[1,2,3,4,5]
    print('input=',input_str)
    str_reverse=str_Reverse(input_str)
    print'output',str_reverse)


沒有留言:

張貼留言