[2016 微軟預科生計劃-探星夏令營在線測試2] Give My Text Back(字符串處理)

描述

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?

輸入

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

輸出

The original text.

樣例輸入
my Name  is Little   Hi.
His   name IS Little ho  ,  We are   friends.
樣例輸出
My name is little hi.
His name is little ho, we are friends.

題解:單詞提取,字符串簡單處理。注意一些細節:每句話第一單詞第一字符大寫,空格等問題。

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
#include<sstream>
#include<cstdio>
#include<unordered_map>
#include<unordered_set>
#include<cmath>
#include<climits>
#include<stack>
#include<queue>
using namespace std;
bool is_c(char c) {
   if(c>='a'&&c<='z'||c>='A'&&c<='Z') return true;
   return false;
}
int main() {
    string s;
    while(getline(cin,s)!=NULL) {
        int len=s.size();
        string res="";
        for(int i=0;i<len;i++) {
            if(s[i]>='A'&&s[i]<='Z') {
                s[i]+=32;
            }
        }
        int start=0;
        bool isCap=true;
        while(true) {
            while(start<len) {
                if(s[start]==','||s[start]=='.') {
                    res+=s[start];
                    if(s[start]=='.') {
                        isCap=true;
                    }
                }
                else if(is_c(s[start])) break;
                start++;
            }
            if(start>=len) break;
            string num="";
            for(int i=start;i<len;i++) {
                if(s[i]==' ') {
                   num=s.substr(start,i-start);
                   start=i+1;
                   break;
                }
                else if(s[i]==','||s[i]=='.') {
                   num=s.substr(start,i-start+1);
                   start=i+1;
                   break;
                }
            }
            if(num.size()==0) {
                break;
            }
            else {
                if(isCap) {
                    num[0]-=32;
                    isCap=false;
                }
                if(res.size()==0) {
                    res+=num;
                }
                else res+=" "+num;
                if(num[num.size()-1]=='.') {
                    isCap=true;
                }
            }
        }
        cout<<res<<endl;
    }
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章