编程练习9

 1 //删除字符串中字符个数最少的字符
 2 #include <iostream>
 3 using namespace std;
 4 #define N 100
 5 
 6 void remove(char* str,char ch)
 7 {
 8     int i=0,j,len=strlen(str);
 9     int k=len;
10     while(i<k)
11     {
12         if(str[i]==ch)
13         {
14             for(j=i;j<k-1;++j)
15             {
16                 str[j]=str[j+1];
17             }
18             str[j]='\0';
19             k--;
20         }
21         else
22             i++;
23     }
24 
25 }
26 void func(char* str)
27 {
28     char tmp[N]={0};
29     int i,j,k=0,len=strlen(str);
30     char* s=(char*)malloc(len*sizeof(char));
31     int* c=(int*)malloc(len*sizeof(int));
32     
33     for(i=0;i<len;++i)
34     {
35         s[i]=0;
36         c[i]=0;
37     }
38 
39     for(i=0;i<len;++i)
40     {
41         for(j=0;j<len;++j)
42         {
43             if(str[i]==s[j]) 
44             {
45                 c[j]++;
46                 break;
47             }
48         }
49         if(j==len)
50         {
51             s[i]=str[i];
52             c[i]=1;
53         }
54     }
55 
56     int min=c[0];
57     for(i=1;i<len;++i)
58     {
59         if(c[i]<min && c[i])
60             min=c[i];
61     }
62 
63     cout<<"delete("<<min<<"):";
64     for(i=0;i<len;++i)
65     {
66         if(c[i]==min && s[i])
67         {
68             cout<<s[i]<<" ";
69             remove(str,s[i]);
70         }
71     }
72 
73     cout<<endl<<"res:"<<str;
74     cout<<endl;
75 }
76 
77 int main()
78 {
79     char str[N];
80     cin>>str;
81     
82     func(str);
83 
84     cout<<endl;
85     return 0;
86 }

 

posted on 2012-09-12 19:46  lyncre  阅读(156)  评论(0)    收藏  举报

导航