给定一个ASCII字符串,查找字符串中,出现了k次的字符。比如,字符串"This is a good day!"中,出现了2次的字符为'a','d','i','o', 's',出现了4次的字符为' '。
Input
第一行是一个正整数n(1<=n<=100),表示下面要进行查找的字符串的数量。其后n行,每行是一个字符串(这里确保字符串的结尾不是空格),和一个数字k,字符串和数字k之间由一个空格隔开。
Output
输出要求按照ASCII码从小到大的顺序输出字符,每个字符用单引号括起来,字符间用逗号隔开。
Sample Input
2
This is a good day! 2
This is a good day! 4
Sample Output
'a','d','i','o','s'
' '
代码如下
#include<stdio.h>
#include<string.h>
#include<math.h>
char a[1000],b[1000];
int main()
{
int n,i=0;
scanf("%d",&n);
for(i=0;i<=127;i++)
{
b[i]=b[i-1]+1;
}
getchar();
while(n--)
{
int j,k=0,p,l=0,t=0,c[1000];
memset(c,0,sizeof(c));
gets(a);
p=strlen(a);
for(i=p-1;i>=0;i--)
{
if(a[i]!=' ')
t=t+(a[i]-48)*pow(10,k++);
else
break;
}
for(j=0;j<i;j++)
{
for(k=0;k<=127;k++)
{
b[k]=k;
if(a[j]==b[k])
c[k]++;
}
}
int w=0;
for(l=0;l<=127;l++)
{
if(c[l]==t)
{
if(w==0)
{
printf("'%c'",b[l]);
w++;
}
else
printf(",'%c'",b[l]);
}
}
printf("\n");
}
return 0;
}