PAT字符串处理题---1009 说反话 (20分)

知道stringstream这个类型就很简单

  • 用于把空格过滤掉
        stringstream ss(s);//初始化ss
	string word;
	int cnt=0;
	while(ss >> word){
		str[cnt++]=word;//将输入的一行字符串依次存入字符串数组中
	}
#include<iostream>
#include<sstream>
#include<string>
#include<cstdio>

using namespace std;


int main(){
	string s;
        getline(cin,s);
	stringstream ss(s);
	string str[1005];
	string word;
	int cnt=0;
	while(ss >> word){
		str[cnt++]=word;
	}
	for(int i=cnt-1;i>=0;i--){
		if(i!=cnt-1) cout<<" ";
		cout<<str[i];
	}
	return 0;
}
原文地址:https://www.cnblogs.com/bingers/p/13079799.html