删除一个字符串中出现次数最少的字符
2016-12-04 15:38
323 查看
【输入】s为输入字符串,Len为输入字符串长度
如输入字符串为“abcdd”,输出为”dd”
字符串中只有小写字母,不含空格且字符串最大长度不超过20
下面贴出程序和想法:
1、第一是如果遇到大于最小字母个数的字母直接存储到字符串中,这样就可以删除字符了。
2、直接处理最小的字母。
如输入字符串为“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 = '\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; }
相关文章推荐
- 华为机试——删除一个字符串中出现次数最少的字符
- 华为2013校园招聘上机笔试题-删除一个字符串中出现次数最少的字符
- 华为2013校园招聘机试题目题4:删除一个字符串中出现次数最少的字符
- 编写一个函数,删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除。
- 神州数码:输入一个字符串: (1)输出出现次数最少的字符 (2)删除出现 4000 次数最少的字符并输出字符串
- 删除一个字符串中出现次数最少的字符
- 【华为OJ】删除一个字符串中出现次数最少的字符
- 【C编程练习】华为2013校园招聘机试题目题4:删除一个字符串中出现次数最少的字符,函数原型为:char * delChar(char *s,int iLen)
- 删除一个字符串中出现次数最少的字符
- 删除一个字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- [华为上机练习题]7.删除字符串中出现次数最少的字符
- 华为OJ(删除字符串中出现次数最少的字符)
- 删除字符串中出现次数最少的字符
- OJ 系列之删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 小程序(十五)删除一个字符串中出现最少的字符
- 删除字符串中出现次数最少的字符
- 求字符串中字符的出现的最多次数和最少次数,删除后并获得最终字符