//本题可以用vector 也可以用 stack
//本题利用栈——先进后出的原则+cin流输入——遇空格就读入之前的内容的特性
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
string str;
stack<string>si;
int flag=1;//首位标示符
while(cin>>str)
{
si.push(str);//入栈
}
while(!si.empty())
{
if(flag)
{
cout<<si.top();
flag=0;
}
else
cout<<" "<<si.top();
si.pop();
}
cout<<endl;
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str;
while(getline(cin,str)){
reverse(str.begin(),str.end());
int i=0,j=i;
while(i<str.size()){
while(i<str.size()&&str[i]==' ')
++i;
j=i;
while(i<str.size()&&str[i]!=' ')
++i;
reverse(str.begin()+j,str.begin()+i);
j=i;
}
cout<<str<<endl;
}
return 0;
} #include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string str;
while(getline(cin,str))
{
int length=str.length();
string temp;
vector<string> vec;
for(int i=0;i<length;i++)
{
if(str[i]!=' ')
temp.push_back(str[i]);
else
{
vec.push_back(temp);
temp.clear(); //这里一定要清除
}
}
vec.push_back(temp); //需要把最后一个单词压入到vec中
for(int j=vec.size()-1;j>0;j--) //倒序输出
cout<<vec[j]<<' ';
cout<<vec[0]<<endl;
}
} #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str;
getline(cin, str);
reverse(str.begin(), str.end());
auto start = str.begin();
while(start != str.end()){
auto end = start;
while(end != str.end() && *end != ' '){
++end;
}
reverse(start, end);
if(end != str.end()){
start = end + 1;
}
else{
start = end;
}
}
cout << str << endl;
return 0;
} #include <iostream>
#include <string>
using namespace std;
int main(){
string s1, s2;
cin >> s1;
while(cin >> s2){
s1 = s2 + ' ' + s1;
}
cout << s1 << endl;
return 0;
} // 整体的思路即为从后往前遍历,遇到单词便其放到res中
#include<iostream>
#include<cctype>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s;
getline(cin, s);
string res;
int l = s.size() - 1, r = s.size() - 1;
for(int i = s.size() - 1; i >= 0; i--)
{
if(i > 0 && s[i] != ' ' && s[i - 1] != ' ')
l--;
else
{
res += s.substr(l, r - l + 1);
l = i - 1;
r = l;
}
}
cout << res;
return 0;
}
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> res;
string str;
getline(cin, str);
int len = str.length();
int startPos = 0;
for (int i = 0; i < len; i++) {
if (str[i] == ' ') {
res.push_back(str.substr(startPos, i-startPos));
// 过滤连续空格,提高连续空格的字符串处理效率
while (i < len - 1 && str[i+1] == ' ') {
i++;
}
startPos = i+1;
}
}
res.push_back(str.substr(startPos));
int size = res.size();
for (int i = size - 1; i >= 0; i--) {
if (i != size - 1) {
cout << " ";
}
cout << res[i];
}
return 0;
}
//总体思路:
//1、先将整个字符串颠倒
//2、再将以空格分割的各个字符串颠倒
#include<bits/stdc++.h>
using namespace std;
//颠倒字符串
void swap(string &s)
{
int len=s.length();
for(int i=0;i<len/2;i++)
{
char c;
c=s[i];
s[i]=s[len-1-i];
s[len-1-i]=c;
}
}
int main()
{
string s;
getline(cin,s);
swap(s);
int spacenum=0;
int len=s.length();
for(int i=0;i<len;i++)
{
if(s[i]==' ')
spacenum++;
}
spacenum++;//记录字符串个数,包括最后一个空格之后的
int start=0;//每个字符串的第一个字符下标
for(int i=0;i<=len;i++)
{
if(s[i]==' '||s[i]=='\0')
{
string str=s.substr(start,i-start);//截取每个字符串
swap(str);//进行字符串颠倒
start=i+1;
cout<<str;
spacenum--;
if(spacenum!=0)
cout<<' ';
else
cout<<endl;
}
}
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
using namespace std;
const int maxn = 105;
string res[maxn];
string p;
int main()
{
getline(cin, p);
stringstream ss;
ss << p;
int count = 0;
while(!ss.eof()) {
ss >> res[++count];
}
for(int i = count; i > 0; i--) {
cout << res[i];
i == 1 ? cout << endl : cout << " ";
}
return 0;
}
// 思路挺简单的,以空格为分界符,将每个单词倒置一下
// 然后将整个字符串倒置一下,因此关键的步骤就是倒置。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
char[] ch = str.toCharArray();
int index = 0;
for(int i=0;i<ch.length;i++){
if(ch[i]==' '){
invert(ch,index,i-1);
index = i+1;
}else if(i==ch.length-1){
invert(ch, index, i);
}
}
invert(ch,0,ch.length-1);
System.out.println(String.valueOf(ch));
}
}
public static void invert(char[] ch, int start, int end){
char c;
while(start<end){
c = ch[start];
ch[start] = ch[end];
ch[end] = c;
start++;
end--;
}
}
}
#include<iostream>
#include<string>
using namespace std;
void reverse(string& s,int index1,int index2)
{
int i=index1,j=index2;
while(i<j)
{
char tmp=s[i];
s[i]=s[j];
s[j]=tmp;
i++;
j--;
}
}
int main()
{
string s;
getline(cin,s);
reverse(s,0,s.size()-1);
int i=0,j=0;
while(j<s.size())
{
while(j<s.size()&&s[j]!=' ')
j++;
reverse(s,i,j-1);
while(j<s.size()&&s[j]==' ')
j++;
i=j;
}
cout<<s<<endl;
return 0;
}