hdu1062 Text Reverse(C语言)
来源:互联网 发布:剑灵身材数据 编辑:程序博客网 时间:2024/06/05 00:29
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.
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
3olleh !dlrowm'I morf .udhI ekil .mca
Sample Output
hello world!I'm from hdu.I like acm.HintRemember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.
Author
Ignatius.L
法一
#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<string.h>int main(){ int t,f,i,j,k; char s[1005]; scanf("%d",&t); getchar(); while(t--) { gets(s); f=strlen(s); for(i=0; i<=f; i++) { if(s[i]==' '||i==f) { for(j=i-1; j!=k&&j>=0; j--) putchar(s[j]); k=i; if(i!=f) putchar(' '); } } printf("\n"); }}然后我又发现了反转函数,其实还可以写一个的再写一个: ),有时间了再写吧。
原型:extern char *strrev(char *s);
用法:#include <string.h>
功能:把字符串s的所有字符的顺序颠倒过来(不包括空字符NULL)。
说明:返回指向颠倒顺序后的字符串指针。
阅读全文
0 0
- hdu1062 Text Reverse(C语言)
- hdu1062(Text Reverse)
- hdu1062 Text Reverse (c++输入输出流)
- HDU1062:Text Reverse
- Text Reverse hdu1062 水题
- hdu1062 Text Reverse
- HDU1062 Text Reverse
- hdu1062:Text Reverse
- HDU1062 Text Reverse
- Hdu1062 Text Reverse
- hdu1062 - Text Reverse
- hdu1062-Text Reverse
- HDU1062 Text Reverse
- HDU1062 Text Reverse
- HDU1062 Text Reverse
- hdu1062——Text Reverse
- 面试100题系列之8单词反转 VS HDU1062——text reverse
- hdu1062-字符串翻转问题 reverse()
- Python文件操作
- delphi中httpencode使用注意事项
- 关于斜杠与反斜杠的区别
- 虚IP
- web.xml文件加载过程微了解
- hdu1062 Text Reverse(C语言)
- [RK3288][Android6.0] 调试笔记 --- 动态显示和隐藏导航栏(广播方式)
- 随便写点关于卡尔曼滤波
- Boost在Windows的使用VS2015配置及使用
- 虚拟机上安装SVN服务
- 二叉搜索树
- 今天突然看到一篇介绍WIN98的文章,才发现原来真的老了
- 数据库——(16)函数
- 一文弄懂神经网络中的反向传播法——BackPropagation