/* * rstrlen -- get length of string recursively * * Matt Bishop, ECS 36A * * April 30, 2024 * -- original program written */ #include /* * macro */ #define MAXLEN 1024 /* max length of input */ /* * recursive string length calculation */ int rstrlen(char *s) { /* base case: if string is empty, length is 0 */ if (*s == '\0') return(0); /* recursion: lop off first char, get length of rest, add 1, return it */ return(1 + rstrlen(s+1)); } /* * main program */ int main(void) { char buf[MAXLEN]; /* input buffer */ char *b; /* pointer used to find \n or end ofinput line */ /* * keep going until you get an EOF */ while(fgets(buf, 1000, stdin) != NULL){ /* clobber the first newline (shouldbe at the end, if present) */ for(b = buf; *b != '\0' && *b != '\n'; b++) ; *b = '\0'; /* print the string and itslength */ printf("string '%s' is %d characters long\n", buf, rstrlen(buf)); } /* do svidanya! */ return(0); }