删除一个字符串中出现次数最少的字符
2016-12-08 17:04
330 查看
【输入】s为输入字符串,Len为输入字符串长度
如输入字符串为“abcdd”,输出为”dd”
字符串中只有小写字母,不含空格且字符串最大长度不超过20
下面贴出程序和想法:
1、第一是如果遇到大于最小字母个数的字母直接存储到字符串中,这样就可以删除字符了。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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; }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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; }
相关文章推荐
- C#调用dll时的类型转换总结
- 【iOS】Illegal Configuration: The Label outlet from the ViewController to the UILabel is invalid. Outl
- 25. Reverse Nodes in k-Group
- 去除dedecms底部中的powerby
- github 使用方法
- 超简单玩转 GitHub 的问题单(issue)
- calltree看代码调用图
- 编译C#代码
- SSH网站实录(6)登录模块添加验证器进行表单验证
- Android高级控件(四)——VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷
- Spring Boot 动态数据源(多数据源自动切换)
- mybatis返回外键对象列的查询方法
- js初始化对象的错误
- 通配符匹配
- SpringMVC4异常处理
- 基于JS实现的随机数字抽签实例
- docker commit 创建镜像
- SpringBoot项目如何访问jsp页面
- oracle or语句的坑
- java应用于二叉树层次输出和之字形输出的非递归实现