0

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

MSU
  • 1
  • 1

1 Answers1

0

Move this part:

if(tab[str[i] - 'a'] < len)
  printf("%d%c, ", tab[str[i] - 'a'], str[i]);    

Out of the while loop and into a for loop of its own:

 for(i=0; i<26; i++)
 {
   if(tab[i] > 0)
     printf("%d%c, ", tab[str[i] - 'a'], str[i]);    
 }

Misc other problems:

  • Use const correctness. void count_alpha(char *str) -> void count_alpha(const char *str).

  • Arithmetic on symbol values such as str[i] - 'a' is strictly speaking not well-defined and isn't guaranteed to work (except for the symbols '0' to '9'). For a fully portable and strictly conforming program, you would instead do int tab[256] = {0}; and then access it with an index corresponding to the symbol value. (Would also make the algorithm case-sensitive and faster, at the expense of stack memory use.)

  • This part here is nonsense, just remove it since it fills no purpose:

    if(tab[str[i] - 'a'] == 0)
      tab[str[i] - 'a'] = 1;
    else
    
  • Making your own function str_len instead of calling standard strlen just makes your program much slower for no good reason.

  • return(i); The parenthesis around the return value is pointless.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • @MSU `if(i != 25) printf(", ");` – Lundin Nov 23 '22 at 12:19
  • thank you @Lundin that's very helpful i added another loop and it works perfectly ` i = 0; while(str[i]) { if(tab[str[i] - 'a'] != 0) printf("%d%c, ", tab[str[i] - 'a'], str[i]); tab[str[i] - 'a'] = 0; i++; } -- so the out put to this: int main() { char str[] = "aaabcc"; count_alpha(str); return(0); } ` -- output -- 3a, 1b, 2c, now only problem I don't want the last comma to appear I want it as 3a, 1b, 2c – MSU Nov 23 '22 at 12:20
  • in this case it would print as below `3a, , , 1b, 2c, ,` the code is `i = 0; while(str[i]) { if(tab[str[i] - 'a'] != 0) printf("%d%c", tab[str[i] - 'a'], str[i]); if (i < 25) printf(", "); tab[str[i] - 'a'] = 0; i++; }` – MSU Nov 23 '22 at 12:32
  • @MSU Did you even read this answer? Move the printing out of the while loop. – Lundin Nov 23 '22 at 13:58