sicily_1323 switch text

题目

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)函数进行读入。

代码

// Problem#: 1323
// Submission#: 2661187
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<stdio.h>
#include<string.h>

// sicily doesn't support the strrev function in <string.h>, thus rewrites.
char* strrev(char *str) {
  char *h = str, *t = str, ch;
  while (*t++) {}
  t--;
  t--;
  while (h < t) {
    ch = *h;
    *h++ = *t;
    *t-- = ch;
  }
  return str;
}

void reverse(char* str) {
  char tmp_str[10000 + 10];
  int mid = strlen(str) / 2;
  memset(tmp_str, 0, sizeof(tmp_str));
  strrev(str);
  strncpy(tmp_str, str, mid*sizeof(char));
  strrev(str);
  if ((strlen(str) % 2)) mid++;
  str[mid] = 0;
  strrev(str);
  strcat(str, tmp_str);
}

bool checkEmptyLine(char* str) {
  for (int i = 0; i < strlen(str); i++) {
    if (str[i] != ' ')
      return true;
  }
  return false;
}

int main() {
  char str1[10000 + 10] = { 0 }, str2[10000 + 10] = { 0 };

  while (gets(str1)) {
    gets(str2);
    if (str2[0] && checkEmptyLine(str2)) {
      reverse(str2);
      printf("%s\n", str2);
    }

    if (str1[0] && checkEmptyLine(str1)) {
      reverse(str1);
      printf("%s\n", str1);
    }

    memset(str1, 0, sizeof(str1));
    memset(str2, 0, sizeof(str2));
  }
  return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,997评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,617评论 25 708
  • 昨天晚上上的那节肚皮舞课是个人上过的人数最少的,没有之一。包括迟到了四分钟的本人在内,一共也才只有4个人。好在有上...
    竞走的蜗牛阅读 501评论 0 1
  • 今天二美给的这个话题真好,简单地想了想,最近还真是有不少小确幸呢!简直信手拈来。原来我离幸福如此之近,或者说我一直...
    行云流水joy阅读 378评论 0 1
  • 地球是籠牢, 是被流放的星球。 我們是被流放的外星人後裔。 人類進化的幾千萬年歷史, 不過是宇宙的笑話。
    寅夕阅读 65评论 0 0