定义一个命名空间Myspace,包含以下函数:将一个字符串中的所有单词进行反转,并输出反转后的结果。例如,输入字符串为"Hello World",输出结果为"olleH dlroW",并在主函数内测试该函数
#include <iostream>
#include<cstring>
using namespace std;
namespace myspace
{
void fun(string str);
}
void myspace::fun(string str)
{
int len = str.length();
cout << len << endl;
int p=0;
int q=0;
int flag=0;
while(str[flag])
{
p=flag;
while(str[p]==' ')
{
p++;
}
q=p;
while(str[q]!=' ' && str[q] !='\0')
{
q++;
}
flag =q;
q--;
while(q > p)
{
char temp;
temp = str[p];
str[p]=str[q];
str[q]=temp;
p++;
q--;
}
}
cout << str <<endl;
}
int main()
{
string str;
getline(cin,str);
myspace::fun(str);
return 0;
}