[关闭]
@DingCao-HJJ 2015-09-21T11:51:53.000000Z 字数 1865 阅读 1070

sicily_1323 switch text

sicily


题目

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB 

Description

The program must switch the text lines in a backward order and split them by the middle, processing the input lines in pairs. If an empty or blank line is found, it is considered as a line but it is not printed out to the output. 

Input

The input will be a text file with text lines. 

Output

Standard output with the switched and splitted lines. 

Sample Input

This lines must be printed backwards and splitted in the middle.
And each line too!
hellow my friend
how are you today
i hope you're fine

be cool, be nice.

Sample Output

 hcae dnA!oot enil
wkcab detnirp eb tsum senil sihT.elddim eht ni dettilps dna sdra
y era wohyadot uo
m wollehdneirf y
oy epoh ienif er'u
,looc eb.ecin eb

思路

题目要求每一行从中间分开行数据,然后前后两部分分别逆序,所以可以预先自定义一个部分逆序的函数,根据索引来进行逆序。

然后又要求成对处理行,所以每次扫入可以先扫入第一行,如果第一行能够扫描进去了,那么就说明存在第二行了,也扫描进去。

因为题目要求对空行也进行扫入,所以要用gets(char* str)函数进行读入。

代码

  1. // Problem#: 1323
  2. // Submission#: 2661187
  3. // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
  4. // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
  5. // All Copyright reserved by Informatic Lab of Sun Yat-sen University
  6. #include<stdio.h>
  7. #include<string.h>
  8. // sicily doesn't support the strrev function in <string.h>, thus rewrites.
  9. char* strrev(char *str) {
  10. char *h = str, *t = str, ch;
  11. while (*t++) {}
  12. t--;
  13. t--;
  14. while (h < t) {
  15. ch = *h;
  16. *h++ = *t;
  17. *t-- = ch;
  18. }
  19. return str;
  20. }
  21. void reverse(char* str) {
  22. char tmp_str[10000 + 10];
  23. int mid = strlen(str) / 2;
  24. memset(tmp_str, 0, sizeof(tmp_str));
  25. strrev(str);
  26. strncpy(tmp_str, str, mid*sizeof(char));
  27. strrev(str);
  28. if ((strlen(str) % 2)) mid++;
  29. str[mid] = 0;
  30. strrev(str);
  31. strcat(str, tmp_str);
  32. }
  33. bool checkEmptyLine(char* str) {
  34. for (int i = 0; i < strlen(str); i++) {
  35. if (str[i] != ' ')
  36. return true;
  37. }
  38. return false;
  39. }
  40. int main() {
  41. char str1[10000 + 10] = { 0 }, str2[10000 + 10] = { 0 };
  42. while (gets(str1)) {
  43. gets(str2);
  44. if (str2[0] && checkEmptyLine(str2)) {
  45. reverse(str2);
  46. printf("%s\n", str2);
  47. }
  48. if (str1[0] && checkEmptyLine(str1)) {
  49. reverse(str1);
  50. printf("%s\n", str1);
  51. }
  52. memset(str1, 0, sizeof(str1));
  53. memset(str2, 0, sizeof(str2));
  54. }
  55. return 0;
  56. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注