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. WebKit Web Inspector增加覆盖率分析和类型推断功能

    WebKit中的Web Inspector(Web检查器)主要用于查看页面源代码.实时DOM层次结构.脚本调试.数据收集等,日前增加了两个十分有用的新功能:覆盖率分析和类型推断.覆盖率分析工具能够可视 ...

  2. 【重要版本】Firefly alpha beta v1.2.2 正式发布

    原地址:http://bbs.gameres.com/thread_220175.html firefly 1.2.2 更新 更新内容: 功能添加       1.动态模块更新       2.部分b ...

  3. cxf2.4.3中jaxb-api.jar、jaxws-api.jar与jdk1.6.0_02不兼容问题

    http://chxiaowu.iteye.com/blog/1243475 Exception in thread "main" java.lang.NoClassDefFoun ...

  4. socket编程在windows和linux下的区别

    如无其它说明,本文所指Linux均表示2.6内核Linux,GCC编译器,Windows均表示Windows XP系统,Visual Studio 2005 sp1编译环境. 下面大概分几个方面进行罗 ...

  5. x+2y+3z=n的非负整数解数

    题目:给一个正整数n,范围是[1,10^6],对于方程:x+2y+3z = n,其中x,y,z为非负整数,求有多少个这样的三元组 (x,y,z)满足此等式. 分析:先看x+2y=m,很明显这个等式的非 ...

  6. 【HDOJ】1075 What Are You Talking About

    map,STL搞定. #include <iostream> #include <string> #include <cstdio> #include <cs ...

  7. 【HDOJ】1204 糖果大战

    题目本身不难.类似于dp.f(i)表示手中现有i颗糖果赢的概率,则下一局赢的概率是p(1-q),下一局输的概率是q(1-p),下一句平手的概率是1-p(1-q)-q(1-p),平手包括两人均答对或答错 ...

  8. 卸载Visual Studio Code后删除右键Open with Code…

    Win+R,输入  regedit  ,点击确认,进入注册表编辑器   Ctrl+F,搜索  Ticino  ,将搜索出来的Ticino都删除就行了

  9. 【HtmlParser】HtmlParser使用

    转载 http://www.cnblogs.com/549294286/archive/2012/09/04/2670601.html HTMLParser的核心模块是org.htmlparser.P ...

  10. 从零开始学习jQuery (十) jQueryUI常用功能实战

    一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案,  即使你会使用jQuery也能在阅读中发现些许秘籍. 本文是实战篇. 使用jQueryUI完成制作网站 ...