HDU 1062 TEXT Reverse(文本反转)
2016-07-15 20:54
393 查看
Text Reverse
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
1062
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
解题思路:每遇一个空格,就表示一个单词,所以从第一个不是空格的字母且紧邻其后是空格或结束符的字母开始逆序输出。
注意:第一个for循环里的j必须在循环外赋初值0,否则输出单词会丢失!
#include<stdio.h>
#include<string.h>
int main()
{
char a[1010],b[1010];
int i,j,n,l,k;
scanf("%d",&n);
getchar();
while(n--)
{
gets(a);
l=strlen(a);
j=0;
for(i=0; i<l; i++)
{
if(a[i]!=' '&&(a[i+1]==' '||a[i+1]=='\0'))
{
k=i+1;
while(k--&&a[k]!=' ')
b[j++]=a[k];
}
if(a[i]==' ')
b[j++]=' ';
}
b[j]='\0';
for(i=0; b[i]!='\0'; i++)
printf("%c",b[i]);
printf("\n");
}
return 0;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
1062
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.
解题思路:每遇一个空格,就表示一个单词,所以从第一个不是空格的字母且紧邻其后是空格或结束符的字母开始逆序输出。
注意:第一个for循环里的j必须在循环外赋初值0,否则输出单词会丢失!
#include<stdio.h>
#include<string.h>
int main()
{
char a[1010],b[1010];
int i,j,n,l,k;
scanf("%d",&n);
getchar();
while(n--)
{
gets(a);
l=strlen(a);
j=0;
for(i=0; i<l; i++)
{
if(a[i]!=' '&&(a[i+1]==' '||a[i+1]=='\0'))
{
k=i+1;
while(k--&&a[k]!=' ')
b[j++]=a[k];
}
if(a[i]==' ')
b[j++]=' ';
}
b[j]='\0';
for(i=0; b[i]!='\0'; i++)
printf("%c",b[i]);
printf("\n");
}
return 0;
}
相关文章推荐
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法解析
- 【python】编程语言入门经典100例--40
- HDU 1020 Encoding
- 原地合并两个有序数组
- 栈的Java实现与单词逆序输出
- C语言实现字符的反转输出
- 35-给个数字,求位数,逆序输出
- 对5位以内的正整数处理(判断他是几位数、顺逆序输出)
- 蓝桥杯 算法提高 逆序排列
- Leetcode: Binary Tree Level Order Traversal II 理解分析
- C语言 单链表的 反序输出 与 反转
- 给一个不多于5位的整数,判断位数并逆序输出
- java 将一个数组逆序输出。
- c++ vector、list 逆序输出
- 单链表逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出