首页 > 其他 > 详细

hihoCoder #1197 Give My Text Back

时间:2015-10-28 14:13:01      阅读:515      评论:0      收藏:0      [点我收藏+]

Description

To prepare for the English exam Little Ho collected many digital reading materials. Unfortunately the materials are messed up by a malware.

It is known that the original text contains only English letters (a-zA-Z), spaces, commas, periods and newlines, conforming to the following format:

1. Each sentence contains at least one word, begins with a letter and ends with a period.

2. In a sentence the only capitalized letter is the first letter.

3. In a sentence the words are separated by a single space or a comma and a space.

4. The sentences are separated by a single space or a single newline.

It is also known the malware changes the text in the following ways:

1. Changing the cases of letters.

2. Adding spaces between words and punctuations.

Given the messed text, can you help Little Ho restore the original text?

Input

A string containing no more than 8192 English letters (a-zA-Z), spaces, commas, periods and newlines which is the messed text.

Output

The original text.

Sample Input

my Name  is Little   Hi.
His   name IS Little ho  ,  We are   friends.

Sample Output

My name is little hi.
His name is little ho, we are friends.

Solution:

 1 #include <cstdio>
 2 #include <cstring>
 3 #define MAXN 10000
 4 
 5 char toUpper(char c) {
 6     if (c >= A && c <= Z) return c;
 7     else return A + (c - a);
 8 }
 9 
10 char toLower(char c) {
11     if (c >= a && c <= z) return c;
12     else return a + (c - A);
13 }
14 
15 int main() {
16     char buf[MAXN];
17     char text[MAXN];
18     while (fgets(buf, MAXN, stdin)) {
19         //printf("%s\n", buf);
20         int len = strlen(buf);
21         int idx = -1;
22         int k = 0;
23         int flag = true;
24         for (int i = 0; i < len; ++i) {
25             
26             if ((buf[i] >= a && buf[i] <= z) || buf[i] >= A && buf[i] <= Z) {
27                 if (idx >= 0) {
28                     k = idx + 1;
29                     idx = -1;
30                 }
31                 if (flag) {
32                     text[k++] = toUpper(buf[i]);
33                     flag = false;
34                 }
35                 else 
36                     text[k++] = toLower(buf[i]);
37             }
38             else if (buf[i] ==  ) {
39                 if (idx < 0) {
40                     idx = k;
41                 }
42                 text[k++] = buf[i];
43             }
44             else if (buf[i] == ,) {
45                 if (idx >= 0) {
46                     k = idx;
47                     idx = -1;
48                 }
49                 text[k++] = buf[i];
50             }
51             else if (buf[i] == .) {
52                 if (idx >= 0) {
53                     k = idx;
54                     idx = -1;
55                 }
56                 text[k++] = buf[i];
57                 flag = true;
58             }
59             else text[k++] = buf[i];
60             
61         }
62         text[k] = \0;
63         printf("%s", text);
64     }
65 
66 }

 

hihoCoder #1197 Give My Text Back

原文:http://www.cnblogs.com/liew/p/4916977.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!