单词翻转
来源:互联网 发布:加入网络作协要求 编辑:程序博客网 时间:2024/05/16 00:34
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
例如输入字符串"I am a student.",则输出"student. a am I"。
还可以采用栈的做法。将字符从后面开始,压入栈中,当遇到空字符或者到末尾时,就出栈。栈空的时候又继续压
这样一直到那个串为空即可。
1 #include<iostream> 2 #include<string> 3 #include<stack> 4 using namespace std; 5 6 7 void reverse_str(string& str) 8 { 9 if(str.empty())10 return;11 string str1;12 stack<char> sta;13 while(1)14 {15 while((!str.empty())&&(str.back()!=' '))16 {17 sta.push(str.back());18 str.pop_back();19 }20 21 while(!sta.empty())22 {23 str1.push_back(sta.top());24 sta.pop();25 }26 if(str.empty())27 break;28 if(str.back()==' ')29 {30 str.pop_back();31 str1.push_back(' ');32 }33 }34 str=str1;35 }36 37 int main()38 {39 string str="i am a student";40 reverse_str(str);41 cout<<str<<endl;42 system("pause");43 }
阅读全文
0 0
- 单词翻转
- 单词翻转
- 单词翻转
- 翻转单词
- 单词翻转
- 翻转单词
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 翻转单词
- 单词翻转
- 单词翻转
- 单词翻转
- 单词翻转
- 数据结构--链表
- netty的简单实现
- System.Environment类
- 哲学家就餐问题
- linux下git环境变量配置
- 单词翻转
- java对象和Map互转及测试
- Codeforces contest 787 recordings
- C++详解Leetcode:106. Construct Binary Tree from Inorder and Postorder Traversal
- 精度丢失
- Codeforces contest 376&375 recordings
- 母函数 入门 + 模板
- 适配器模式
- [PAT乙级]1070. 结绳(25)