khalfella

ansi_c_pg46_ch02_ex04.c

Nov 2nd, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. /*
  2. * Exercise 2-4.
  3. * Write an alternative version of squeeze(s1,s2) that deletes each character
  4. * in s1 that matches any character in the string s2.
  5. */
  6.  
  7.  
  8. #include <stdio.h>
  9.  
  10.  
  11. int squeeze(char *s1, const char* s2) {
  12. int i, j;
  13. int k = 0;
  14.  
  15. for (i = 0; s1[i] ; i++) {
  16. for (j = 0; s2[j]; j++) {
  17. if (s1[i] == s2[j]) {
  18. break;
  19. }
  20. }
  21. if (!s2[j])
  22. s1[k++] = s1[i];
  23. }
  24. s1[k] = '\0';
  25. }
  26.  
  27. int any(char *s1, const char* s2) {
  28. int i, j;
  29.  
  30. for (i = 0; s1[i]; i++) {
  31. for (j = 0; s2[j]; j++) {
  32. if (s1[i] == s2[j]) {
  33. return i;
  34. }
  35. }
  36. }
  37.  
  38. return -1;
  39. }
  40.  
  41. int
  42. main(int argc, char** argv) {
  43.  
  44. printf ("any(%s,%s) = %d\n", argv[1], argv[2], any(argv[1], argv[2]));
  45.  
  46. squeeze(argv[1], argv[2]);
  47. printf ("squeeze = %s\n", argv[1]);
  48.  
  49. return (0);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment