1、输入带空格的字符串 使用getline接收输入;

2、带空格和不带空格的情况,使用find()函数查找空格字符,没有找到直接输出整个字符串;

3、带空格的字符串需要逆序输出,从后往前查找空格rfind()返回从后往前的第一个空格的下标,截取空格下标后一个字符到字符串结尾的子字符串用于输出;删除输出的截取的字符串,继续下一次的查找,可用while循环,判断条件为find()返回值是否小于0;

C++ 两种方法;STL的解法巧妙
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    string str;
    //getline(cin, str);
    vector<string> vec;
    while (cin >> str) {  // 这种利用空格作为输入结束的标志很巧妙
        vec.push_back(str);
    }
    
    for (auto it = vec.rbegin(); it != vec.rend(); ++it) {  // 逆序迭代器输出
        cout << *it << " ";
    }
    cout << endl;
    /*
    while (str.find(' ') > 0) { // 查找不到返回 -1;
        int pos = str.rfind(' ');  // 从后往前查找
        if (pos < 0) {  // 查找不到返回 -1;
            break;
        }
        int len = str.length();
        string tmpstr = str.substr(pos + 1, len);
        str = str.erase(pos, len);
        cout << tmpstr << " ";
    }
    cout << str << endl;  // 查找不到空格字符则输出整个字符串
    */
    return 0;
}
}