Posted By

jaircazarin on 10/10/08


Tagged

Algorithms Microsoft-Interview


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

jaircazarin
robe
sergeizen
skammer


Delete Characters.


 / Published in: C
 

Given string a and b. Delete all characters of a that are in b.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. char* deleteCharacters(char * str, char * charSet)
  6. {
  7. int hash [256];
  8. if(NULL == charSet)
  9. return str;
  10. for(int i = 0; i < 256; i++)
  11. hash[i] = 0;
  12. for(int i = 0; i < strlen(charSet); i++)
  13. hash[charSet[i]] = 1;
  14. int currentIndex = 0;
  15. for(int i = 0; i < strlen(str); i++)
  16. {
  17. if(!hash[str[i]])
  18. str[currentIndex++] = str[i];
  19. }
  20. str[currentIndex] = '\0';
  21. return str;
  22. }
  23.  
  24. int main()
  25. {
  26. char s[2] = "a";
  27. char s2[5] = "aca";
  28. printf("%s", deleteCharacters(s2, s));
  29. return 0;
  30. }

Report this snippet  

You need to login to post a comment.