The Cost of Recursion
By Jon Bentley
Dr. Dobb's Journal June 1998
<B>(a)</B> int istrcmp(char *s, char *t) { for ( ; *s == *t; s++, t++) if (*s == 0) return 0; return *s - *t; } <B>(b)</B> int rstrcmp(char *s, char *t) { if (*s == *t) { if (*s) return rstrcmp(s+1, t+1); else return 0; } else return (*s - *t); } <B>(c)</B> int rcstrcmp(char *s, char *t) { return (*s == *t) ? (*s ? rcstrcmp(s+1, t+1) : 0) : *s - *t; }
Example 2: (a) strcmp function that compares two strings; (b) recursive version; (c) implementing the recursive length.
Copyright © 1998, Dr. Dobb's Journal