Description
给出一个字符串,串中有一些不相交的子串被{},[],()分开,现在按{},(),[]的顺序将括号内的子串按顺序输出,首字母要大写,逗号后的串首字母也要大写
Input
一个长度小于10^4的字符串
Output
输出改变顺序后的串
Sample Input
(We) [are] {blind}, if (we) [could not see] {creation of this clone army}
Sample Output
Blind we are, if creation of this clone army we could not see
Solution
字符串处理,用三个string存储三种括号内子串的值,首先将所有字符都转化为小写,遇到逗号再将花括号内字符串首字母改成大写输出
Code
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
char s[11111];
while(gets(s))
{
int len=strlen(s);
for(int i=0;i<len;i++)//将所有大写字母转化为小写字母
if(s[i]>='A'&&s[i]<='Z')
s[i]+=32;
string a,b,c,d;//a,b,c,d分别存储花括号,小括号,方括号内子串,d记录不在括号内的子串
int i=0,flag=0;
while(i<len)
{
if(s[i]=='{')
{
while(i<len&&s[++i]!='}')
a+=s[i];
}
else if(s[i]=='(')
{
while(i<len&&s[++i]!=')')
b+=s[i];
}
else if(s[i]=='[')
{
while(i<len&&s[++i]!=']')
c+=s[i];
}
else if(s[i]==',')
{
a[0]-=32;//将花括号内子串首字母转化为大写后输出
flag=1;//标记逗号是否出现过
cout<<a<<" "<<b<<" "<<c<<",";
a=b=c=d="";
i++;
while(s[i]!='{'&&s[i]!='['&&s[i]!='('&&s[i]!=',')
d+=s[i++];
cout<<d;//将不在括号内的子串输出
i--;
}
i++;
}
if(!flag)//如果逗号没有出现过则将花括号内子串首字母转化为大写后输出
a[0]-=32;
cout<<a<<" "<<b<<" "<<c<<endl;
}
return 0;
}