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<cstring>
#include<string>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
string s;
int i,cnt,j;
//char ss[9000];
while(getline(cin,s))
//while(gets(ss))
{
//s=(ss);
for(i=0;i<s.length();i++)
{
cnt=0;
if(s[i]==' '&&i==0)
{
j=i;
while(s[j]==' ')
{
cnt++;
j++;
}
if(cnt>=1)
{
s.erase(i,cnt);
}
i--;
}
else if(s[i]==' '&&i>0)
{
j=i;
while(s[j]==' '&&j<s.length())
{
cnt++;
j++;
}
if(cnt>1&&j!=s.length())
{
s.erase(i,cnt-1);
}
else
s.erase(i,cnt);
}
else if(s[i]==',')
{
if(s[i-1]==' ')
{
s.erase(i-1,1);
i--;
}
if(s[i+1]!=' ')
{
s=s.substr(0,i+1)+' '+s.substr(i+1);
i++;
}
}
else if(s[i]=='.')
{
if(s[i-1]==' ')
{
s.erase(i-1,1);
i--;
}
if(i==s.length()-1)
continue;
else
{
if(s[i+1]!=' ')
{
s=s.substr(0,i+1)+' '+s.substr(i+1);
i++;
}
}
}
}
if(s[s.length()-1]!='.')
//s[s.length()]='.';
s+='.';
//cout<<s<<endl;
transform(s.begin(), s.end(), s.begin(), ::tolower);
s[0]=s[0]-32;
for(i=1;i<s.length()-1;i++)
{
if(s[i]=='.')
{
s[i+2]=s[i+2]-32;
}
}
cout<<s<<endl;
}
}