dos_compilers/Borland Turbo C++ v1/EXAMPLES/EX4.CPP
2024-07-02 07:34:51 -07:00

35 lines
851 B
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ex4.cpp: Default arguments and Pass-by-reference
// from Chapter 6 of User's Guide
#include <iostream.h>
#include <ctype.h>
int get_word(char *, int &, int start = 0);
main()
{
int word_len;
char *s = " These words will be printed one-per-line ";
int word_idx = get_word(s,word_len); // line 13
while (word_len > 0)
{
cout.write(s+word_idx, word_len);
cout << "\n";
//cout << form("%.*s\n",word_len,s+word_idx);
word_idx = get_word(s,word_len,word_idx+word_len);
}
}
int get_word(char *s, int& size, int start)
{
// Skip initial whitespace
for (int i = start; isspace(s[i]); ++i);
int start_of_word = i;
// Traverse word
while (s[i] != '\0' && !isspace(s[i]))
++i;
size = i - start_of_word;
return start_of_word;
}