#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    getline(cin, s);
    int len = s.length();
    for(int i = len-1;i>=0;i--){
        if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z')){
            // s[i] = ' ';
            continue;
        }
        else{
            s[i] = ' ';
            for(int j = i+1;j<len;j++){
                if(s[j]==' '||s[j]=='\0'){
                    break;
                }
                cout<<s[j];
            }
            cout<<' ';
        }
    }
    for(int i = 0;s[i]!=' '&&s[i]!='\0';i++){
        cout<<s[i];
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

逆序遍历,与第一周那个题不同的是,需要判断非法字符,让非法字符变为空格即可,

活动地址https://www.nowcoder.com/discuss/726480854079250432