CRCK, array 1.2

Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)

// This question is for C/C++ programmer.
// #include <algorithm> if programming in C++. otherwise, need to write an extra swap function.
void reverseString(string input) {
    // first check the input length.
    if(input.size() <= 1) return;
    int left = 0, right = input.size() - 1;
    while(left < right) {
        swap(input[left], input[right]);
        left++;
        right--;
} 


 
 


你可能感兴趣的:(CRCK, array 1.2)