注意题目要求:不能有元音字母,重复的字母只出现一次,标点符号(只有, 。 ?)前后不能有空格,句子的开头结尾不能有空格,连续的空格只能出现一次,具体看注释!!
Description
A krunched word has no vowels ("A", "E", "I", "O", and "U") and no repeated letters. Removing vowels and letters that appear twice or more from MISSISSIPPI yields MSP. In a krunched word, a letter appears only once, the first time it would appear in the unkrunched word. Vowels never appear.
Krunched phrases similarly have no vowels and no repeated letters. Consider this phrase:
and its krunched version:
Blanks are krunched differently. Blanks are removed so that a krunched phrase has no blanks on its beginning or end, never has two blanks in a row, and has no blanks before punctuation. Otherwise, blanks not removed. If we represent blanks by "_",
krunches to:
where the single remaining blank is shown by "_".
Write a program that reads a line of input (whose length ranges from 2 to 70 characters), and krunches it. Put the krunched word or phrase in the output file. The input line has only capital letters, blanks, and the standard punctuation marks: period, comma, and question mark.
Krunched phrases similarly have no vowels and no repeated letters. Consider this phrase:
RAILROAD CROSSING
and its krunched version:
RLD CSNG
Blanks are krunched differently. Blanks are removed so that a krunched phrase has no blanks on its beginning or end, never has two blanks in a row, and has no blanks before punctuation. Otherwise, blanks not removed. If we represent blanks by "_",
MADAM_I_SAY_I_AM_ADAM__
krunches to:
MD_SY
where the single remaining blank is shown by "_".
Write a program that reads a line of input (whose length ranges from 2 to 70 characters), and krunches it. Put the krunched word or phrase in the output file. The input line has only capital letters, blanks, and the standard punctuation marks: period, comma, and question mark.
Input
A single line to be krunched.
Output
A single krunched line that follows the rules above.
Sample Input
NOW IS THE TIME FOR ALL GOOD MEN TO COME TO THE AID OF THEIR COUNTRY.
Sample Output
NW S TH M FR L GD C Y.
代码:
#include<string>
#include<iostream>using namespace std;
int main()
{
bool a[27];
memset(a,false,sizeof(a));
a['A'-'A']=true; //不能有元音字母
a['E'-'A']=true;
a['I'-'A']=true;
a['O'-'A']=true;
a['U'-'A']=true;
char b[150],d,r[150];
cin.getline(b,150,'\n');
int len,i,c,j;
len=strlen(b);
j=0;
d=' ';
for(i=0;i<len;i++)
{
if(b[i]>='A' && b[i]<='Z')
{
c=b[i]-'A';
if(a[c]==false) //字母只能出现一次
{
r[j]=b[i];
j++;
a[c]=true;
d=b[i];
}
}
else if(b[i]==' ')
{
if(d!=' '&& d>='B' &&d<='Z' ) //连续的空格只能出现一次,且标点符号后面没有空格
{
r[j]=b[i];
j++;
d=b[i];
}
}
else
{
r[j]=b[i];
j++;
}
}
for(i=j-1;i>=0;i--)
{
if(r[i]!=' ') //句子后面没有空格
{
len=i;
break;
}
}
for(i=0;i<len;i++)
{
if(r[i]!=' ') //句子前面没空格
{
j=i;break;
}
}
for(i=j;i<len;i++)
{
if((r[i+1]==',' || r[i+1]=='.' || r[i+1]=='?') && r[i]==' ' )
{
continue;
}
else
cout<<r[i];
}
cout<<r[len]<<endl;
return 0;
}