home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / ECKELT / 5 / ITS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-23  |  3.2 KB  |  88 lines

  1. // Non-printed example in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: ITS.CPP -- Find "it's" & 2 enclosing words
  34. #include <fstream.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37. #include "..\allege.h"
  38.  
  39. // Emit count words to cout:
  40. void emitword(char* s, int count = 1) {
  41.   for(int i = 0; i < count; i++) {
  42.     while(!isspace(*s) && *s != 0) cout << *s++;
  43.     while(isspace(*s) && *s != 0) cout << *s++;
  44.   }
  45. }
  46.  
  47. // Move the argument pointer forward by words:
  48. void forwardword(char*& s, int count = 1) {
  49.   for(int i = 0; i < count; i++) {
  50.     while(!isspace(*s) && *s != 0) s++;
  51.     while(isspace(*s) && *s != 0) s++;
  52.   }
  53. }
  54.  
  55. // Move the argument pointer back by words:
  56. void backwardword(char*& s, int count = 1) {
  57.   for(int i = 0; i < count; i++) {
  58.     while(!isspace(*s) && *s != 0) s--;
  59.     while(isspace(*s) && *s != 0) s--;
  60.     while(!isspace(*s) && *s != 0) s--;
  61.     s++; // Point to start of word
  62.   }
  63. }
  64. // Char* c; // debug aid
  65. main(int argc, char* argv[]) {
  66.   allege(argc >= 2,
  67.     "usage: its textfile word(=it's) pad(=1)\n"
  68.     "finds the word and the pad words "
  69.     "surrounding it for grammar syntax check");
  70.   const char* word = (argc >= 3 ? argv[2] : "it's");
  71.   const int pad = (argc == 4 ? atoi(argv[3]) : 1);
  72.   ifstream in(argv[1]);
  73.   allegefile(in);
  74.   const sz = 4096;
  75.   char _buf[sz + 10] = {0}; // Set to zero
  76.   char* const buf = _buf + 10; // Pad beginning w/ zeroes
  77.   while(in.getline(buf,sz)) {
  78.     char* c = buf;
  79.     while((c = strstr(c, word)) != 0) {
  80.       backwardword(c, pad);
  81.       emitword(c, 2*pad + 1);
  82.       cout << endl;
  83.       forwardword(c, 2*pad + 1);
  84.     }
  85.   }
  86. }
  87.  
  88.