C++ preliminary: namespace, string string

Define a namespace Myspace, including the following function: reverse all words in a string and output the reversed result. For example, the input string is "Hello World", the output result is "olleH dlroW", and the function is tested within the main function.

#include <iostream>

using namespace std;


//创建命名空间
namespace my_namespace {

    string fun();
}
//引入命名空间
using namespace my_namespace;

string fun(string str)
{
    int start = 0;
    int end = str.length()-1;
    //逆置
    while (start<end)
    {
        char temp = str.at(start);
        str.at(start) = str.at(end);
        str.at(end) = temp;
        start++;
        end--;
    }

    return str;
}

int main()
{
    //定义一个字符串
    string str;
    cout << "请输入字符串" << endl;
    //读取字符串
    getline(cin, str);

    //调用逆置函数
    //fun(str);
    cout << fun(str)<< endl;
    cout << str <<endl;   //疑问点??
    return 0;
}

2. Mind map

 

Guess you like

Origin blog.csdn.net/qq_46766479/article/details/132417027