给定一个字符串以及该字符串的子串,将字符串反转,但子串部分不反转,示例如下。
输入字符串为"Welcome you, my friend"
子串为"you"
输出为"dneirf ym ,you emocleW
#include<iostream>
#include<stack>
#include<cassert>
using namespace std;
char* reverse(const char* welcome,const char* token,char *verse)
{
assert(welcome && token && verse);
const char* real=welcome;
const char* head=welcome;
const char* ken=token;
stack<char> stack1;
while(*head!='/0')
{
while(*head!='/0' && *ken==*head)
{
ken++;
head++;
}
if(*ken=='/0')
{
const char* p;
for(p=head-1;real<=p;p--)
stack1.push(*p);
ken=token;
real=head;
}
else
{
stack1.push(*real++);
head=real;
ken=token;
}
}
int j=0;
bool test=stack1.empty();
while(!stack1.empty())
{
verse[j++]=stack1.top();
stack1.pop();
}
verse[j]='/0';
return verse;
}
int main()
{
char welcome[]="welcome you,my friend";
char token[]="you";
cout<<welcome<<endl;
cout<<token<<endl;
int i=strlen(welcome);
char *verse=new char(i+1);
char *returnchar=reverse(welcome,token,verse);
cout<<returnchar<<endl;
verse=0;
delete[] verse;
return 0;
}