fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. char* remove_duplicated(char* str, int size) {
  5. int frequency[256] = {0};
  6. char* new_str = malloc(size);
  7. int new_size = 0;
  8.  
  9. for(int i=0; str[i]!='\0'; i++)
  10. {
  11. if(frequency[(unsigned char) str[i]] == 0) {
  12. frequency[(unsigned char) str[i]]++;
  13. new_str[new_size] = str[i];
  14. new_size++;
  15. }
  16. }
  17.  
  18. new_str[new_size] = '\0';
  19.  
  20. return new_str;
  21. }
  22.  
  23. int main(void) {
  24.  
  25. char str[30] = "aaabbbcccAXXYYZZZZ";
  26. int count[30] = {0};
  27. char* new_str = remove_duplicated(str, 30);
  28.  
  29. for(int i=0; new_str[i] != '\0'; i++)
  30. {
  31. for(int j=0; str[j]!='\0'; j++)
  32. {
  33. if(new_str[i]==str[j])
  34. count[i]++;
  35. }
  36. }
  37.  
  38. for(int i=0; new_str[i]!='\0'; i++)
  39. printf("%c occurs %d times \n", new_str[i], count[i]);
  40.  
  41. free(new_str);
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 2300KB
stdin
Standard input is empty
stdout
a occurs 3 times 
b occurs 3 times 
c occurs 3 times 
A occurs 1 times 
X occurs 2 times 
Y occurs 2 times 
Z occurs 4 times