字符串中单词翻转实现
2016-04-10 01:58
507 查看
#include<stdio.h>
#include<iostream>
#include<string>
#include<queue>
#include<stack>
using namespace std;
int main(){
stack<string> stack_str;
string str;
string temp;
char temp_ch[100];
while(1){
str="";
cout<<"input string"<<endl;
getline(cin,str);
if(str.size()==0||str.size()<0){
cout<<"input invalide\n";
cout<<"once again"<<endl;
continue;
}
int temp_locate=0;
for(int i=0;;i++){
if(str[i]==' '||i==str.size()){
temp_ch[temp_locate]='\0';
temp=temp_ch;
stack_str.push(temp);
cout<<"temp="<<temp<<endl;
temp="";
temp_locate=0;
if(i==str.size()) break;
while(temp_ch[i+1]==' ') i++;
}
else{
temp_ch[temp_locate++]=str[i];
}
}
str="";
while(!stack_str.empty()){
cout<<"stack_str.top()="<<stack_str.top()<<endl;
temp=stack_str.top();
stack_str.pop();
str+=temp;
if(stack_str.size()==0){
str+=" ";
}
}
cout<<"str="<<str<<endl;
}
return 0;
}
#include<iostream>
#include<string>
#include<queue>
#include<stack>
using namespace std;
int main(){
stack<string> stack_str;
string str;
string temp;
char temp_ch[100];
while(1){
str="";
cout<<"input string"<<endl;
getline(cin,str);
if(str.size()==0||str.size()<0){
cout<<"input invalide\n";
cout<<"once again"<<endl;
continue;
}
int temp_locate=0;
for(int i=0;;i++){
if(str[i]==' '||i==str.size()){
temp_ch[temp_locate]='\0';
temp=temp_ch;
stack_str.push(temp);
cout<<"temp="<<temp<<endl;
temp="";
temp_locate=0;
if(i==str.size()) break;
while(temp_ch[i+1]==' ') i++;
}
else{
temp_ch[temp_locate++]=str[i];
}
}
str="";
while(!stack_str.empty()){
cout<<"stack_str.top()="<<stack_str.top()<<endl;
temp=stack_str.top();
stack_str.pop();
str+=temp;
if(stack_str.size()==0){
str+=" ";
}
}
cout<<"str="<<str<<endl;
}
return 0;
}
相关文章推荐
- 【2】mysql 数据库的DDL语句
- WEB前端优化的体会
- Android资源----ShapeDrawable
- PHP中 应该如何写 无符号右移
- 20145229吴姗珊 《Java程序设计》第6周学习总结
- iOS 自定义TabBarController
- 需警惕CSS3属性的书写顺序
- 20145308刘昊阳 《Java程序设计》第6周学习总结
- Spark工程开发前台技术实现与后台函数调用
- Spark函数详解系列之RDD基本转换
- OS开发UI篇—UITabBarController简单介绍
- 重装系统最纯净最完整版教程,不用ghost
- 电面:给一个数组找出和为k的两个数
- Android中的Intent Filter匹配规则介绍
- Java split用法
- hdu1009(简单贪心)
- [LeetCode]259. 3Sum Smaller
- WordPress 迈向 Markdown 的脚步
- 【九度OJ】题目1111:单词替换
- 安卓的主要几大布局