home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-3.ZIP / EXAMPLES.ZIP / EX4.CPP < prev    next >
C/C++ Source or Header  |  1992-02-18  |  915b  |  38 lines

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