字符串反转
对一句话里面的单词进行reverse,标点符号位置不变。符号包括:,.!?
#include <stack>
#include <cstdio>
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
stack<char> a;
getline(cin,str);
int len=str.length();
for(int i=0;i<len;i++)
{
if(str[i]==' '||str[i]==','||str[i]=='.'||str[i]=='?'||str[i]=='!'||str[i]=='\n')
{
string word="";
while(!a.empty())
{
char ch=a.top();
a.pop();
word+=ch;
}
int wordlen=word.length();
str.replace(i-wordlen,wordlen,word);
}
else
{
a.push(str[i]);
}
}
cout<<str<<endl;
return 0;
}
原文链接