Finding the frequency of each characters

1 min read
#Finding frequency of each character in an string without using the hash-map…
// printing the frequency of each character..without using hashmap..
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin,s);
vector<int> v(26,0);//frequency array..special array..
for(int i=0;i<s.length();i++){
char ch = s[i];
int x = (int)ch;
int idx = x-97;
v[idx]+=1;
}
for(int i=0;i<v.size();i++){
if(v[i]!=0){
int ascii = 97+i;
cout<<(char)ascii<<" "<<v[i]<<endl;
}
}
return 0;
}
You can also do it easily using a char and integer map .. bye bye ..
0
Subscribe to my newsletter
Read articles from Arpan Tiwari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
