fork download
  1. #include <stdlib.h>
  2. #include<iostream>
  3. using namespace std;
  4. #define NO_OF_CHARS 256
  5.  
  6. bool* getCharCountArray(char *str)
  7. {
  8. bool *count = (bool *)calloc(sizeof(bool), NO_OF_CHARS);
  9. for (int i = 0; str[i]; i++)
  10. count[ str[i] ] = 1;
  11. return count;
  12. }
  13.  
  14. char *removeDirtyChars(char *str, char *mask_str)
  15. {
  16. bool* count = getCharCountArray(mask_str);
  17.  
  18. int ip_ind = 0, res_ind = 0;
  19. while ( str[ip_ind] )
  20. {
  21. char temp = str [ip_ind];
  22. if (count[temp] == 0)
  23. str [res_ind++] = str [ip_ind];
  24. ip_ind++;
  25. }
  26.  
  27. str[res_ind] = '\0';
  28. return str;
  29. }
  30.  
  31. int main()
  32. {
  33. char str[] = "geeksforgeeks";
  34. char mask_str[] = "mask";
  35. cout<< removeDirtyChars(str, mask_str) <<endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 4484KB
stdin
Standard input is empty
stdout
geeforgee