c++实现句子翻转
例如:are you ok ! => ! ok you are
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
string fun(string str)
{
string temp;
char space;
int count = 0;
string s;
for(int i = 0;i < str.length();i++)
{
char aa = str[i];
if(aa == space)//判断是否为连续空格
{
continue;
}
else
{
space = NULL;
temp += str[i];
}
if(aa == ' ' || i == str.length() -1)
{
space = aa;
if(count == 0)//判断是否为第一次
{
s.append(temp);
count++;
}
else
{
string w;
w = s;
s = "";
s.append(temp + " ");
s.append(w);
}
temp = "";
}
}
return s;
}
int main()
{
string str;
str = fun(" are you ok !");
cout << str << endl;
return 0;
}