【C语言】删除指定字符并计算出现次数并重新排序
#include <stdio.h>
#include<string.h>
#define N 20
int main()
{
char a[N],b[N];
char temp;
int i,j;
int count=0;
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='c')
{
count++;
for(j=i;j<strlen(a)-1;j++)
{
a[j]=a[j+1];
}
i--;
a[j]='\0';
}
}
if(count!=0)
{
printf("count:%d\n",count);
}
else if(count==0)
{
printf("No c\n");
}
for(i=0;a[i]!='\0';i++)
{
b[i]=a[i];
}
b[i]='\0';
printf("b:");
puts(b);
for(i=1;b[i]!='\0';i++)
{
for(j=0;j<strlen(b)-i;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
printf("b sorted:");
puts(b);
return 0;
}
程序功能:按示例格式完成以下要求:
(1)输入一行字符(不超过20个),存入一维字符数组a中;
(2)输出小写字母'c'出现的次数,若数组中没有'c',也要报告"No c";
(3)输出去掉小写字母'c'后的新数组b;
(4)按字符ASCII码值从小到大依次输出数组b。
如输入:
abcdABCDcc589
则输出:
count:3
b: abdABCD589
b sorted: 589ABCDabd
说明:输出的第一行为小写字母'c'出现的次数;第二行为去掉小写字母'c'之后的新数组b;
第三行为按字符ASCII码值从小到大输出的数组b。
又如输入:
aABCDdefg789
则输出:
No c
b: aABCDdefg789
b sorted: 789ABCDadefg
说明:输出的第一行为数组中不含小写字母'c'的提示;第二行为新数组b;
第三行为按字符ASCII码值从小到大输出的数组b。