1.2 Write code to reverse a C-Style String.

时间:2021-08-25 22:25:44

Write code to reverse a C-Style String.


#include<iostream>
#include<cstring>

using namespace std;
void swap(char &a, char &b){
    a = a^b;
    b = a^b;
    a = a^b;
}

void reverse(char *str) {
    int n = strlen(str);
    for (int i = 0; i < n/2; i++) {
        swap(str[i], str[n-1-i]);
    }
    return ;
}

int main() {
    char str[] = "01234567";
    cout<<"the origin str : "<<str<<endl;
    reverse(str);
    cout<<"the reverse str : "<<str<<endl;
    return 0;

}