Some code I just found. Simple matching algorithms. I love playing with pointers in C

uncommon.c

  1. #include <stdio.h>
  2.  
  3.  
  4. int main( int argc , char ** argv , char ** env )
  5. {
  6. if (argc != 3)
  7. {
  8. fprintf(stdout, "Usage: %s <palabraX> <palabraY>\n\r", argv[0]);
  9. return -1;
  10. }
  11.  
  12. /* find out the size for the registry */
  13. char * registry = NULL;
  14. registry = (char*)malloc( strlen(argv[1]) * sizeof(char) + 1);
  15. memset(registry, 0, sizeof(registry) + 1);
  16.  
  17. char * wordX = argv[1], *wordY = argv[2], *startY = wordY;
  18.  
  19. while ( *wordX )
  20. {
  21. while( *wordY )
  22. {
  23. if (*wordX == *wordY)
  24. break;
  25.  
  26. wordY++;
  27. }
  28.  
  29. if (!*wordY)
  30. {
  31. char *rr = registry;
  32. while(*rr) rr++;
  33. *rr = *wordX;
  34. }
  35.  
  36. wordY = startY;
  37. wordX++;
  38. }
  39.  
  40. fprintf(stdout, "output: %s\n\r", registry);
  41.  
  42. free(registry);
  43. return 0;
  44. }

common.c

  1. #include <stdio.h>
  2.  
  3.  
  4. int main( int argc , char ** argv , char ** env )
  5. {
  6. if (argc != 3)
  7. {
  8. fprintf(stdout, "Usage: %s <palabraX> <palabraY>\n\r", argv[0]);
  9. return -1;
  10. }
  11.  
  12. /* find out the size for the registry */
  13. char * registry = NULL;
  14.  
  15. registry = (char*)malloc( strlen(argv[1]) * sizeof(char) + 1);
  16. memset(registry, 0, sizeof(registry) + 1);
  17.  
  18. char * wordX = argv[1], *wordY = argv[2], *startY = wordY;
  19.  
  20. while ( *wordX )
  21. {
  22.  
  23. while (*wordY)
  24. {
  25. if (*wordX == *wordY)
  26. {
  27. char * rr = registry;
  28.  
  29. while( *rr )
  30. {
  31. if (*rr == *wordY)
  32. break;
  33. ++rr;
  34. }
  35.  
  36.  
  37. if (!*rr)
  38. {
  39. *rr = *wordY;
  40. break;
  41. }
  42. wordY++;
  43. }
  44. else
  45. wordY++;
  46. }
  47.  
  48. wordX++;
  49. wordY = startY;
  50. }
  51.  
  52.  
  53. fprintf(stdout, "registry: %s\n\r", registry);
  54. free(registry);
  55.  
  56. return 0;
  57. }

Tags: