// 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;
}
本文通过一个简单的C++程序演示了如何实现字符串的整体反转以及基于特定字符的子串局部反转。通过对字符数组的操作,采用异或交换法实现了高效的空间无额外开销的字符串反转。此方法不仅适用于整体字符串的反转,还能灵活地应用于子串级别的反转操作。
632

被折叠的 条评论
为什么被折叠?



