6.27 C++初级 作业

  1. 整理思维导图

 

2.定义一个命名空间Myspace,包含以下函数:将一个字符串中的所有单词进行反转,并输出反转后的结果。例如,输入字符串为"Hello World",输出结果为"olleH dlroW",并在主函数内测试该函数。

6.27 C++初级 作业_第1张图片

#include 

using namespace std;

namespace Myspace
{
    void fanzhuan (string str)
    {
        int i = 0;
        int j = 0;
        char temp;
        int flag = 0;
        while(str[flag])
        {
            i = flag;
            while(str[i]==' ')
            {
                i++;
            }
            j=i;
            while(str[j]!=' ' && str[j]!='\0')
            {
                j++;
            }
            flag = j;
            j--;
            while(j>i)
            {
                temp = str[i];
                str[i] = str[j];
                str[j] = temp;
                j--;
                i++;
            }
        }
        cout << str << endl;
    }

}

int main()
{
    using namespace Myspace;
    string str = "Hello World";
    fanzhuan(str);
    return 0;
}

 

你可能感兴趣的:(c++,开发语言)