高效的字符串反轉函數

c++

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char s[] = "abcdefghigklmnopqrstuvwxyz";

char c;
for(int i=0,j=strlen(s)-1;i<j;++i,--j)
{
c = s[i];
s[i] = s[j];
s[j] = c;

}

for(int n=0; n<strlen(s);n++)
{
   cout<< s[n];
}

}

將第1個與最後一個替換 第二個與倒數第二個替換 一直到中間爲止




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章