删除一个字符串中出现次数最少的字符

【输入】s为输入字符串,Len为输入字符串长度
如输入字符串为“abcdd”,输出为”dd”
字符串中只有小写字母,不含空格且字符串最大长度不超过20
下面贴出程序和想法:
1、第一是如果遇到大于最小字母个数的字母直接存储到字符串中,这样就可以删除字符了。

#include <stdio.h>
#include <string.h>

char *deClear(char *str,int len)
{
    int i;
    int minnum;
    int count[26] = {0};
    static char pstr[100];

    for(i = 0; i < len; i++)
    {
        count[str[i] - 'a']++;
    }
    for( i = 0; i < len; i++)
    {
        if(count[i] != 0)
        { 
            minnum = count[i];
        }
    }
    for(i = 0; i < len; i++)
    {
        if(count[i] < minum && count[i] != 0)
        {
            minnum = count[i];
        }
    }

    for(i = 0; i < len; i++)
    {
         if(count[str[i] - 'a'] > minnum)
         {
              pstr[n++] = str[i];
         }
         pstr[n] = '\0';
    }
    return pstr;

}

int main()

{
    char str[100];
    int count[26] = {0};
    printf("please input the string.h\n");
    scanf("%s",str);

    int len = strlen(str);
    deClear(str,count,len);
    printf("the result is %s\n",str);

    reuturn 0;
}

2、直接处理最小的字母。

#include <stdio.h>

void delete_ch(char *str);
{
    int i;
    int len = strlen(str);
    for(i = 0; i < len; i++)
    {
        *(str + i) = *(str + i + 1);
    }
    *(str + len - 1) = '\0';
}

void get_count_ch(int *count,char *str)
{
    int i = 0;
    int len = strlen(str);
    for(i = 0; i < len; i++)
    {
        count[*(str +i ) - 'a']++;
    }
 }
int get_min_count_ch(int *count)
{
    int i;
    int minnum;
    for(i = 0; i < 26; i++)
    {
        if(count[i] != 0)
        {
            minnum = count[i];
            for(; i < 26; i++)
            {
                if(count[i] < minnum && count[i] != '\0')
                {
                    minnum = count[i];
                }
            }
         }
   }

   return minnum;

}

void delete_min_count_ch(char *str,int minnum);
{
     while(*str != '\0')
     {
         if(count[*str - 'a'] == minnum)
         {
             delete_ch(str);
         }
         else
         {
              str++;
         }
     }
}
int main()
{
    char str[100];
    int count[26] = {0};


     printf("please input the string:\n");
     scanf("%s",str);

     int len = strlen(str);
     get_count_ch(count,str,len);
     int min = get_min_count_ch(count);
     delete_min_count_ch(str,min);

     printf("the result is %s\n",str);

     return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值