character array

guys i have a problem i cant find a way to
count how many times a character occur in a character array
the out put should be
a=2
b=1
but my output is
a=2
b=1
a=0
i cant find away to get rid of the other a letter
i dont know what to do


#include <iostream>
using namespace std;
int main()
{
char array[3] = {'a','b','a'};
char frequency[26] ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int forchar[3]={0};
int u=0;
for(int i=0; i<26; i++)
{

for(int y=0; y<3; y++)
{

if(frequency[i]==array[y])
{
++forchar[u];
}


}
u++;

}
for(int x=0; x<3; x++)
{

cout<<array[x]<<" "<<forchar[x]<<endl;
}


}
Last edited on
you can use array container in c++.

#include <iostream>
#include <string>
#include <algorithm>
#include<array>
using namespace std;

int main(){
array<char,7> ca{'a','b','c','b','d','b','d'};
cout<<count(ca.begin(),ca.end(),'b');
return 0;
}

output is=3
yes its working sujitnag. thankx
Topic archived. No new replies allowed.