close
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 ;
}
文章標籤
全站熱搜
留言列表