Comma quibbling: Difference between revisions

Content deleted Content added
→‎Tcl: Added implementation
No edit summary
Line 43: Line 43:
{ A, B, C and D }
{ A, B, C and D }
{ A, B, C, D and E }</pre>
{ A, B, C, D and E }</pre>

=={{header|C++}}==
<lang cpp>#include <iostream>

template<class T>
void quibble(std::ostream& o, T i, T e) {
o << "{";
if (e != i) {
T n = i++;
const char* more = "";
while (e != i) {
o << more << *n;
more = ", ";
n = i++;
}
o << (*more?" and ":"") << *n;
}
o << "}";
}

int main(int argc, char** argv) {
char const* a[] = {"ABC","DEF","G","H"};
for (int i=0; i<5; i++) {
quibble(std::cout, a, a+i);
std::cout << std::endl;
}
return 0;
}
</lang>

{{out}}
<pre>
{}
{ABC}
{ABC and DEF}
{ABC, DEF and G}
{ABC, DEF, G and H}
</pre>


=={{header|D}}==
=={{header|D}}==