Question : write code to reverse a c-style string ( C-String mean that "abcd " is represented as five characters , including the null character ). 

 

 

Ans :

 

 

#include <iostream>
using namespace std;


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

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

int main()
{
    char str[] = "123456789 Eric";
    reverse(str);
    cout << str << endl ; 
}

arrow
arrow
    文章標籤
    reverse c c++
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()