String compression

why this code is not passing 2nd and 3rd test case
#include
#include
using namespace std;
int main() {
string s;
int freq[256]={0};
cin>>s;
for(int i=0;i<s.length();i++)
{
freq[s[i]]++;
}
char ch;
for(int i=0;i<256;i++)
{
if(freq[i]>0)
{
cout<<char(i)<<freq[i];
}
}
return 0;
}

@dreamerekta please save your code on ide.codingblocks.com and share the link here

@dreamerekta your logic is flawed because
for inputs like
aabbaa
the answer should be a2b2a2 but your program will not give an output like this.

now i have sort the string …and it is giving correct output for inputs like you told me…but yet not passing the test cases 2nd and 3rd
#include
#include
#include
using namespace std;
int main() {
string s;
int freq[256]={0};
cin>>s;
sort(s.begin(),s.end());
for(int i=0;i<s.length();i++)
{
freq[s[i]]++;
}
char ch;
for(int i=0;i<256;i++)
{
if(freq[i]>0)
{
cout<<char(i)<<freq[i];
}
}
return 0;
}

@dreamerekta please save your code on ide.codingblocks.com and share the link, I have asked you before as well. Sharing code like this changes the formatting and the indentation is gone and a lot of keywords also go missing.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.