can someone advise how can I print the repeating letter with its final count/occurance once? I have the following code and after it the output but I want the output to be: 2a, 1b, 3c instead of 1a, 2a, 1b, 1c, 2c, 3c
#include <unistd.h>
#include <stdio.h>
int str_len(char *str)
{
int i = 0;
while(str[i])
i++;
return(i);
}
void count_alpha(char *str)
{
int tab[26] = {0};
int i = 0;
int len = str_len(str);
while(str[i])
{
if (str[i] >= 'a' && str[i] <= 'z')
{
if(tab[str[i] - 'a'] == 0)
tab[str[i] - 'a'] = 1;
else
tab[str[i] - 'a']++;
}
if(tab[str[i] - 'a'] < len)
printf("%d%c, ", tab[str[i] - 'a'], str[i]);
i++;
}
}
int main()
{
char str[] = "aabccc";
count_alpha(str);
return(0);
}
output
1a, 2a, 1b, 1c, 2c, 3c, %
i need a simple way to write the letter just once with its final occurrence count