HDU_1062Text Reverse
来源:互联网 发布:医院没有编制 知乎 编辑:程序博客网 时间:2024/05/08 06:53
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.
代码:
思路解析:每个空格后的字符反转,本题只有空格这一个特殊字符,以他为标记,记录单词。切记空格的输出要判断再输出。。。
#include<iostream>#include <stdio.h> #include <string.h> using namespace std; int main() { int n,i,j,m,a,b; char str[1000],s[1000][1000],z; cin>>n; getchar(); while(n--) { gets(str); memset(s,'\0',sizeof(s)); //s[]初始化为‘\0’,字符串的结束符 a = b = 0; m = strlen(str); for(i = 0; i < m;i++) { if(str[i] == ' ') { a++; //a计数空格 b = 0; //每次遇到空格都更新为“0”. continue; } s[a][b] = str[i];//用s[a][b]数组来存放每个空格隔断的字符串 b++; //计数空格后的字符 } for(i = 0;i <= a;i++) { int w = strlen(s[i]); //每个空格后的字符串 for(j = 0;j<w/2;j++)//字符串的反转 { z = s[i][j]; s[i][j] = s[i][w-j-1]; s[i][w-j-1] = z; } printf("%s",s[i]); if(i!=a)//输出空格 printf(" "); } printf("\n"); } return 0; }
思路解析:每个空格后的字符反转,本题只有空格这一个特殊字符,以他为标记,记录单词。切记空格的输出要判断再输出。。。
0 0
- HDU_1062Text Reverse
- reverse
- reverse
- Reverse
- reverse()
- 【reverse】
- reverse
- reverse
- reverse()
- reverse
- Reverse Ajax
- Reverse string
- reverse()翻转
- Reverse engineering
- Reverse Text
- Poisoned Reverse
- reverse mapping
- Reverse byte
- HTML自学笔记(一)
- C++ - 类的成员变量 声明顺序 与 初始化顺序 相同
- 关于Cloud 9 IDE初次访问提示“Session ID missing”的bug
- JAVA开发CORBA(学习笔记)
- 如何在GridView中实现多选
- HDU_1062Text Reverse
- C++ 构造函数中能调用虚函数吗
- java集合框架的构成及分类
- 程序员学习能力提升三要素
- iOS中如何处理memory warning
- android IPC通信机制中BBinder与BpBinder的区别
- Raw-OS源码分析之系统初始化
- Java中final关键字的使用方法
- Error: syntax error, unexpected 'endforeach' (T_ENDFOREACH)