您的位置:首页 > 其它

字符串中单词翻转实现

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: