Listing 6 A maxlen function intended for both arrays of constant strings and arrays of non-constant strings
#include <iostream.h> #include <string.h> #define DIM(a) (sizeof(a)/sizeof(a[0])) char *quintet[] = { "flute", "oboe". "horn", "clarinet", "bassoon" }; const char *quartet[] = { "violin", "violin", "viola", "cello" }; const char *const trio[] = { "washtub", "jaw harp", "kazoo" }; size_t maxlen(const char *const *t, size_t n) { size_t len = 0; size_t sl = 0; size_t i; for (i = 0; i < n; ++i) if ((sl = strlen(t[i])) > len) len = sl; return len; } int main() { cout << maxlen(quintet, DIM(quintet)) << endl; cout << maxlen(trio, DIM(trio)) << endl; return 0; } /* End of File */