- 定义自己的命名空间my_sapce,在my_sapce中定义string类型的变量s1,再定义一个函数完成对字符串的逆置。
#include <iostream> #include <string.h> using namespace std; namespace my_space { string s1; void RevString(string &s1); } void my_space::RevString(string &s1) { int i = 0; int j = s1.size()-1; while(i<j) { char temp=s1.at(i); s1.at(i)=s1.at(j); s1.at(j)=temp; i++; j--; } } int main() { cout << "请输入字符串: " << endl; getline(cin,my_space::s1); my_space::RevString(my_space::s1); cout << "逆置后的字符串: " << endl; cout <<my_space::s1 << endl; cout << endl; return 0; }