如输入为“abc”
输出为"cba"
- //vs2005
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <stack>
- using namespace std;
- void reverse(char *str)
- {
- char *p,*q;
- string s1,s2;
- s1=str;
- p=str;
- q=str;
- int m=0,n=0;
- cout<<s1<<endl;
- stack<string> sstack;
- while(*q!='\0')
- {
- while(*q!=' ')//找到单词结束位置
- q++,n++;
- while(*q==' ')//找到单词开始位置
- q++,n++;
- s2=s1.substr(m,n-m);
- sstack.push(s2);
- p=q;
- m=n;
- }
- while(!sstack.empty())
- {
- cout<<sstack.top()<<' ';
- sstack.pop();
- }
- printf("\n");
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- char *str="i come from tianjin.";
- reverse(str);
- return 0;
- }