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. 增加Android可用内存

    In the development of TV applications, especially when dealing with images were more likely to feel ...

  2. Cocos2d-x 3.2编译Android程序错误的解决方案

    最近的升级Cocos2d-x 3.2正式版.iOS不管是什么程序编译问题,使用结果cocos compile -p android编译APK计划.结果悲剧,出现以下错误. Android NDK: I ...

  3. 第九章 Mass Storage设备

    9.1 Mass Storage设备介绍 USB的Mass Storage类是USB大容量储存设备类(Mass Storage Device Class).专门用于大容量存储设备,比如U盘.移动硬盘. ...

  4. delphi xe3的helper语法 good

    在C#中有一个很有用的helper保留字,它可以让我们对已有的类添加额外功能,当时就在想delphi有这个保留字就好了,这样许多控件就不需要继承重写了.后来delphi 果然有了这个语法,到delph ...

  5. Android自定义属性时format选项可以取用的值

    1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name="名称"> <attr format=" ...

  6. 下拉列表联动显示(Car表) 三级联动

    .Models namespace 下拉列表联动显示_Car表_.Models { public class ProductorBF { private MyDBDataContext _contex ...

  7. POJ2104 区间第k小

    题意就是区间第k大…… 题解: 前段时间用主席树搞掉了…… 如今看到划分树,是在想来写一遍,结果18号对着学长的代码调了一上午连样例都没过,好桑心…… 今天在做NOI2010超级钢琴,忽然发现用划分树 ...

  8. VMwareWorkstation10安装OS_X_Mavericks10.9.2图文详细教程

    一.VMware的环境配置...        1.1安装VMware的MAC OS补丁...        1.2建立虚拟机... 二.OS_X_Mavericks的安装及安装驱动...      ...

  9. CVirtualGridCtrl控件内的数据如何获取

    CVirtualGridCtrl控件是同花顺自己写的控件和网上的不同,难处理,可以通过 模拟输入ctrl+c,然后从clipboard提取内容.

  10. 尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。

    从10G开始,Oracle提供了一个较为轻量级的客户包,叫做Instant Client. 将它安装好后,就不用再安装庞大的Oracle Client了. 这样一来,只要客户端下载Instant Cl ...