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. 1006: [HNOI2008]神奇的国度

    图上的最小的染色方案: 学习了陈丹绮的论文: MCS算法 #include<cstdio> #define maxn 10005 #define maxm 2000005 using na ...

  2. sql 中 case when 语法(转)

    sql语言中有没有类似C语言中的switch case的语句?? 没有,用case   when   来代替就行了.            例如,下面的语句显示中文年月         select ...

  3. TSS 任务状态段

    TSS(任务状态段) 1 什么是TSS TSS 全称task state segment,是指在操作系统进程管理的过程中,任务(进程)切换时的任务现场信息.  2 TSS工作细节 TSS在任务切换过程 ...

  4. JAVA实现word doc docx pdf excel的在线浏览 - 仿百度文库 源码

    我们具体实现思路是这样的 首先下载并安装openoffice和swftools openoffice下载地址:http://www.openoffice.org/download/index.html ...

  5. 【POJ】1505 Copying Books

    此题主要采用DP思路,难点是求解"/",需要考虑划分数量不够的情况,先采用DP求解最优解,然后采用贪心求解slash.防止中间结果出错,使用了unsigned int. #incl ...

  6. hadoop2.2基准测试

    <hadoop the definitive way>(third version)中的Benchmarking a Hadoop Cluster Test Cases的class在新的版 ...

  7. Memcached‘do_item_get’函数安全漏洞

    漏洞名称: Memcached‘do_item_get’函数安全漏洞 CNNVD编号: CNNVD-201401-175 发布时间: 2014-01-15 更新时间: 2014-01-15 危害等级: ...

  8. BZOJ1334: [Baltic2008]Elect

    1334: [Baltic2008]Elect Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 386  Solved: 201[Submit][Sta ...

  9. servlet读取cookie问题

    String sessionid = request.getSession().getId(); // 取得当前的session id ckSessionid = new Cookie("s ...

  10. 负载均衡服务器session共享的解决方案

    在ASP.NET的程序中要使用Session对象时,必须确保页面的@page指令中EnableSessionState属性是True或者Readonly,并且在web.config文件中正确的设置了S ...