异或翻转字符串:String:Improve my method of reverse a string

来源:互联网 发布:温州动车事故真相 知乎 编辑:程序博客网 时间:2024/06/06 00:14

 // TestString.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 char s[]="How are you!!";
 int len=strlen(s);
 int high=len-1;
 for(int m=0;m<high;m++,high--)
 {
       s[m]^=s[high];
    s[high]^=s[m];
    s[m]^=s[high];
 }
for(int p=0;p<len;p++)
  cout<<s[p]<<" ";
cout<<endl;
 int start=0,end=0;
    for(int i=0;i<len;i++)
 {
  if(s[i]=='.'||s[i]=='!'||s[i]==':'||s[i]==',')
   start+=1;
  if(s[i]==' ')
   end=i-1;
  if(i==len-1)
   end=i;
  for(int j=start,k=end;j<k;j++,k--)
  {
   s[j]^=s[k];
   s[k]^=s[j];
   s[j]^=s[k];
  }
  start=end+1;
 }
 for(int p=0;p<len;p++)
  cout<<s[p]<<" ";
 cin.get();
 return 0;
}

 

原创粉丝点击