C++统计每一个字符在字符串中出现的次数
//Number of each character repetition in a string //programming by : Erfan Nasoori //Email : ketn68@yahoo.com //Date of send : 2009/1/21 #include <iostream.h> #include <conio.h> void main() { char *s; int n,i,j,flag[1000],c; for(i=0;i<1000;++i) flag[i]=0; s=new char[1000]; cout<<"Please enter your string:"<<endl; cin.ignore(1,'\n'); cin.getline(s,1000); for(i=0;s[i];++i) { c=0; if(flag[i]==0) { for(j=i+1;s[j];++j) if(flag[j]==0 && s[i]==s[j]) { flag[j]=1; ++c; } if(s[i]!=' ') cout<<s[i]<<" : "<<c+1<<"\n"; else cout<<"Space: "<<c+1<<"\n"; } } getch(); }