D
DFS
My original:
too much VB-inspired I see
Barry Schwarz:
his submission added a for loop to walk the list of delimiters one at a time
Ben Bacarisse:
no for loops, no local counter variables, no strlen(), uses pointer
arithmetic?, uses strcspn() and strspn() C library functions
//==========================================================================
int getMaxWordLength_DFS(char *str, char *delim) {
int i=0,j=0,maxWordLen=0;
for(i=0;i<=strlen(str);i++) {
if(str==delim[0] || str==delim[1] || str==delim[2] ||
str=='\0') {
if((i-j) > maxWordLen) {
maxWordLen = (i-j);
}
j = i + 1;
}
}
return maxWordLen;
}
//==========================================================================
int getMaxWordLength_BarryS(const char *str, const char *delim) {
int i=0, i2=0, j=0,maxWordLen=0;
for(i=0;i<=strlen(str);i++) {
for (i2 = strlen(delim); i2 >= 0; i2--) {
if (str == delim[i2]) {
if((i-j) > maxWordLen) {
maxWordLen = (i-j);
}
j = i + 1;
}
}
}
return maxWordLen;
}
//==========================================================================
int getMaxWordLength_BenB(const char *str, const char *delim) {
size_t max = 0;
while (*str) {
size_t l = strcspn(str, delim);
if (l > max) max = l;
str += l + strspn(str, delim);
}
return max;
}
//==========================================================================
Choose your prize, wisely:
http://www.pinterest.com/pin/96897829457913032/
http://purescans.com/bng/
too much VB-inspired I see
Barry Schwarz:
his submission added a for loop to walk the list of delimiters one at a time
Ben Bacarisse:
no for loops, no local counter variables, no strlen(), uses pointer
arithmetic?, uses strcspn() and strspn() C library functions
//==========================================================================
int getMaxWordLength_DFS(char *str, char *delim) {
int i=0,j=0,maxWordLen=0;
for(i=0;i<=strlen(str);i++) {
if(str==delim[0] || str==delim[1] || str==delim[2] ||
str=='\0') {
if((i-j) > maxWordLen) {
maxWordLen = (i-j);
}
j = i + 1;
}
}
return maxWordLen;
}
//==========================================================================
int getMaxWordLength_BarryS(const char *str, const char *delim) {
int i=0, i2=0, j=0,maxWordLen=0;
for(i=0;i<=strlen(str);i++) {
for (i2 = strlen(delim); i2 >= 0; i2--) {
if (str == delim[i2]) {
if((i-j) > maxWordLen) {
maxWordLen = (i-j);
}
j = i + 1;
}
}
}
return maxWordLen;
}
//==========================================================================
int getMaxWordLength_BenB(const char *str, const char *delim) {
size_t max = 0;
while (*str) {
size_t l = strcspn(str, delim);
if (l > max) max = l;
str += l + strspn(str, delim);
}
return max;
}
//==========================================================================
Choose your prize, wisely:
http://www.pinterest.com/pin/96897829457913032/
http://purescans.com/bng/