代码改变世界

百度笔试题-revert函数

2009-10-05 08:58  Iron  阅读(230)  评论(0编辑  收藏  举报
#include <iostream>
using namespace std;
void revert(char * str, int length)
{
 int last;
 for (int i=0;i<length;++i)
 {
  if(str[i]=='\0')
  {
   last = i;
   break;
  }
 }
 char temp;
 for (int i=0;i<last-1;++i,--last)
 {
  temp = str[i];
  str[i] = str[last-1];
  str[last-1] = temp;
 }
}
int main()
{
 char s[] = "abcdef";
 revert(s,_countof(s));
 cout << s << endl;
}