Text Reverse

                                                                                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

 
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case contains a single line with several words. There will be at most 1000 characters in a line.

 
Output
For each test case, you should output the text which is processed.

 
Sample Input
3
olleh !dlrow
m'I morf .udh
I ekil .mca
 
Sample Output
hello world!
I'm from hdu.
I like acm.

Hint

Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.

 
题意:输入一个字符串,将字符串中的 单词反转后输出,一次反转一个。
法一:用数组保存单词。
      将不是空格的字符保存在一个数组中,当遇到空格时,将这个数组中的元素从后往前输出。
#include<stdio.h>
#include<string.h>
int main()
{
int i,n,len,j,k,t;
char s1[1005],s2[100];
scanf("%d",&n);
getchar();
while(n--)
{
gets(s1);
len=strlen(s1);
for(i=0,j=0,t=0;i<len;i++)
{
if(s1[i]!=' ')
s2[j++]=s1[i]; /*保存单词*/
else
{
if(t>0) printf(" "); /*控制格式*/
for(k=j-1;k>=0;k--)
printf("%c",s2[k]); /*反转输出*/
j=0;
t++;
}
if(i==len-1) /*反转最后一个单词,这里要特别注意*/
{
printf(" ");
for(k=j-1;k>=0;k--)
printf("%c",s2[k]);
}
}
printf("\n");
}
return 0;
}

法二:用栈。

       单词反转就是把组成这个单词的字母逆序输出,刚好符合栈的“先进后出,后进先出”特性。压栈时,一次压入一个字符。
#include<stdio.h>
#include<stack>
using namespace std;
int main()
{
int n;
char ch;
scanf("%d",&n);
getchar(); /*吸收回车符*/
while(n--)
{
stack<char> s; /*定义栈*/
while(true)
{
ch=getchar(); /*压栈时,一次压入一个字符*/
if(ch==' '||ch=='\n'||ch==EOF)
{
while(!s.empty())
{
printf("%c",s.top());
s.pop(); /*清除栈顶元素*/
}
if(ch=='\n'||ch==EOF)
break; /*绝对不能少,控制输出结束*/
printf(" ");
}
else
s.push(ch);
}
printf("\n");
}
return 0;
}

hdu 1062 Text Reverse 字符串的更多相关文章

  1. HDOJ/HDU 1062 Text Reverse(字符串翻转~)

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  2. HDU 1062 Text Reverse(水题,字符串处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. #include< ...

  3. 题解报告:hdu 1062 Text Reverse

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 Problem Description Ignatius likes to write word ...

  4. HDU 1062 Text Reverse

    题意 : 给出你一个句子,让你把句子中每个单词的字母顺序颠倒一下输出. 思路 : 用栈即可,就是注意原来在哪儿有空格就要输出空格. //hdu1062 #include <iostream> ...

  5. [hdu 1062] Text Reverse | STL-stack

    原题 题目大意: t组数据,每组为一行,遇到空格时讲前面的单词反转输出. 题解: 显然的栈题,遇到空格时将当前栈输出清空即可 #include<cstdio> #include<st ...

  6. HDOJ 1062 Text Reverse

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. 1062 Text Reverse

    http://acm.hdu.edu.cn/showproblem.php?pid=1062 思路: 最主要的是通过getline函数存取字符串. 如何读取单个单词,并且反向输出? 用\n作为单个单词 ...

  8. 【HDOJ】1062 Text Reverse

    Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignati ...

  9. 简单字符串处理 hdu1062 Text Reverse

    虽然这个题目一遍AC,但是心里还是忍不住骂了句shit! 花了一个小时,这个题目已经水到一定程度了,但是我却在反转这个操作上含糊不清,并且还是在采用了辅助数组的情况下,关系的理顺都如此之难. 其实我是 ...

随机推荐

  1. 静态分析安全测试(SAST)优缺点探析

    静态分析安全测试(SAST)是指不运行被测程序本身,仅通过分析或者检查源程序的语法.结构.过程.接口等来检查程序的正确性,那么采用静分析安全测试的方法有什么优缺点呢,且让小编给你说道说道. 许多公司都 ...

  2. 对 APM 用户的一次真实调查分析(上)

    一.前言 国内的 APM 行业这两年刚刚起步,但是在国外却比较成熟了,并且由于这两年人力成本的快速提高,国内外涌现了几家非常不错的 APM 企业,例如,OneAPM,APPdynamic,Dynami ...

  3. ZOJ 3713 In 7-bit

    点我看题目 题意 : 这个题的英文叙述真的是太强了,真不知道哪里来的英文,完全看不懂,看了两个小时没弄懂真正的题意.就是给你一个字符串,先输出长度,但是长度要用二进制表示出来,二进制的低7位左边如果没 ...

  4. 解读分库分表中间件Sharding-JDBC

    [编者按]数据库分库分表从互联网时代开启至今,一直是热门话题.在NoSQL横行的今天,关系型数据库凭借其稳定.查询灵活.兼容等特性,仍被大多数公司作为首选数据库.因此,合理采用分库分表技术应对海量数据 ...

  5. 图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案

    版权声明:本文为博主原创文章,未经博主允许不得转载. 06-03 20:45:24.143: E/AndroidRuntime(1230): FATAL EXCEPTION: main06-03 20 ...

  6. Android 动态背景的实现以及SurfaceView中添加EditText控件

    首先还是一贯作风,我们先看案例: \ 静态图看不出来效果,如果用过此软件(扎客)的同学们都知道,她的背景会动.怎么样,是不是觉得很时尚,起码比静态的要好(个人观点).其实实现起来并不复杂,这个如果让做 ...

  7. java的异常处理机制(try…catch…finally)

    1 引子try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解.不过,我亲自体验的“教训”告诉我,这个东西可不是想象中的那么简单.听话.不信 ...

  8. 手势识别官方教程(2)识别常见手势用GestureDetector+手势回调接口/手势抽象类

    简介 GestureDetector识别手势. GestureDetector.OnGestureListener是识别手势后的回调接口.GestureDetector.SimpleOnGesture ...

  9. bzoj1143 2718

    最小可相交路径覆盖 先预处理可到达的点然后转化为最小不相交路径覆盖 type node=record        point,next:longint;      end; ..] of node; ...

  10. 关于“javax.servlet.include.request_uri”属性值

    在springMVC的DispatcherServlet类的doService方法中有如下代码: if (WebUtils.isIncludeRequest(request)) { attribute ...