定义一个命名空间Myspace,包含以下函数:将一个字符串中的所有单词进行反转,并输出反转后的结果。例如,输入字符串为"Hello World",输出结果为"olleH dlroW",并在主函数内测试该函数。
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
namespace my_namespace
{
void String_inversion();
}
void my_namespace::String_inversion()
{
string str;
cout << "请输入字符串:";
getline(cin,str);
int i=0,j=0;
int len = str.size();
while(i<len)
{
while(j<len && str.at(j)!=' ')
{
j++;
}
int k=j-1;
while(i<k)
{
char t = str.at(i);
str.at(i) = str.at(k);
str.at(k) = t;
i++;
k--;
}
while(j<len && str.at(j) == ' ')
{
j++;
}
i=j;
}
cout << "逆置:" << str << endl;
}
using namespace my_namespace;
int main()
{
String_inversion();
return 0;
}
思维导图