Text Reverse
3 olleh !dlrow m‘I morf .udh I ekil .mca
hello world! I‘m from hdu. I like acm.HintRemember to use getchar() to read ‘\n‘ after the interger T, then you may use gets() to read a line and process it.
#include<cstdio> #include<stack> #include<cstring> using namespace std; int main(){ int t; scanf("%d",&t); getchar();//吸收回车 while(t--){ char x; stack<char>f; while(1){ x=getchar(); if(x!=' '&&x!='\n'&&x!=EOF) f.push(x); else{ while(!f.empty()){ printf("%c",f.top()); f.pop(); } if(x=='\n'||x==EOF) break; printf(" "); } } printf("\n"); } return 0; }
用C字符串流代码如下:
#include<cstdio> #include<sstream> #include<iostream> using namespace std; int main(){ int T; string s,ss; scanf("%d",&T); getchar(); while(T--){ getline(cin,s); int x=0; while(s[x]==' '){//开头可能有空格 printf(" "); x++; } istringstream sin(s); while(sin>>ss){ x+=ss.length(); for(int i=ss.length()-1;i>-1;i--) printf("%c",ss[i]); while(x<s.length()&&s[x]==' '){//中间空格可能不是一个 printf(" "); x++; } } printf("\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 1062.Text Reverse【栈或数组或字符串流】【字符处理】【8月30】
原文地址:http://blog.csdn.net/a995549572/article/details/48104701