I have created a program for finding the duplicate character in a string. It is throwing garbage please help me to find the error and also find out its solution. I have created two arrays array a and b array a is a character array and array b is a integer array. array a is storing the characyeer that occurred in the string and array b for storing their frequency. please help me to find out the error
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string s;
int i,j,k,l,m,n=0;
cout<<"Enter a string:";
cin>>s;
while (s[i]!='\0')
{
j++;
i++;
}
char *a=(char *)malloc(j*sizeof(char));
int *b = (int *)malloc(j*sizeof(int));
for (i=0;i<j;i++)
{
b[i]=0;
}
for (i=0;i<j;i++)
{
l=0;
for (k=0;k<i;k++)
{
if (s[i]==a[k])
{
b[k]++;
}
else
{
l++;
}
}
if (l+1==i)
{
a[i]=s[i];
b[i]++;
n++;
}
}
i=0;
while (a[i]!='\0')
{
m++;
i++;
cout<<a[i];
}
for (i=0;i<m;i++)
{
if (b[i]>1)
{
cout<<a[i]<<"occurs "<<b[i]<<" times";
}
}
return 0;
}