home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / EX4.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  854 b   |  35 lines

  1. // ex4.cpp:   Default arguments and Pass-by-reference
  2. // from Chapter 6 of Getting Started
  3. #include <iostream.h>
  4. #include <ctype.h>
  5.  
  6. int get_word(char *, int &, int start = 0);
  7.  
  8. main()
  9. {
  10.    int word_len;
  11.    char *s = "  These words will be printed one-per-line  ";
  12.  
  13.    int word_idx = get_word(s,word_len);           // line 13
  14.    while (word_len > 0)
  15.    {
  16.       cout.write(s+word_idx, word_len);
  17.                 cout << "\n";
  18.       //cout << form("%.*s\n",word_len,s+word_idx);
  19.       word_idx = get_word(s,word_len,word_idx+word_len);
  20.    }
  21. }
  22.  
  23. int get_word(char *s, int& size, int start)
  24. {
  25.    // Skip initial whitespace
  26.    for (int i = start; isspace(s[i]); ++i);
  27.    int start_of_word = i;
  28.  
  29.    // Traverse word
  30.    while (s[i] != '\0' && !isspace(s[i]))
  31.       ++i;
  32.    size = i - start_of_word;
  33.    return start_of_word;
  34. }
  35.