fork(1) download
  1. #include <stdio.h>
  2. #define MAX_CHARS 256
  3.  
  4. char * remove_duplicates(char *s){
  5.  
  6. int hash[MAX_CHARS],i;
  7. char *source, *destination;
  8. if(s == NULL) return NULL;
  9.  
  10. source = s;
  11. destination = s;
  12. /* Initialize hash */
  13. for(i=0;i<MAX_CHARS; i++) hash[i]=0;
  14.  
  15. while(*source != '\0'){
  16. /* If this is first time character is encountered */
  17. if(hash[*source] == 0){
  18. hash[*source]++;
  19. *destination = *source;
  20. destination++;
  21. source++;
  22. }
  23. /* We have already processed this character. */
  24. else{
  25. source++;
  26. }
  27. }
  28. /*Imp : Terminate the string */
  29. *destination = '\0';
  30. return s;
  31. }
  32. int main(void) {
  33. // your code goes here
  34. char str[10] = "aaabccc";
  35. printf("%s\n", remove_duplicates(str));
  36.  
  37. char str1[] ="abcdef";
  38. printf("%s\n", remove_duplicates(str1));
  39.  
  40. char str2[] = "";
  41. printf("%s\n", remove_duplicates(str2));
  42.  
  43. if(remove_duplicates(NULL) != NULL){
  44. printf("%s\n", remove_duplicates(NULL));
  45. }
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
abc
abcdef