当前位置: 58彩票app下载 > 关于计算机 > 正文

统计每一个字符在字符串中出现的次数

时间:2019-09-24 12:48来源:关于计算机
//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.hvoid main(){ char *s; int n,i,j,flag[1000],c; for(i=0;i1000;++i) flag[i]
//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();
}

编辑:关于计算机 本文来源:统计每一个字符在字符串中出现的次数

关键词: