What is this code doing? C/C++ -
can tell recursive function doing. think nothing, returns 0 , program stops.
for string: honolulu\0
int fun(char *string){ if(*string != 'u') return 0; return 1 + fun(string + 1); }
it counts number of 'u's @ start of string.
cout << fun("umbrella"); //prints: "1" cout << fun("uumbrella"); //prints: "2"
note assumes string '\0' terminated. many old c string functions iterate off end of array until finds random '\0' somewhere in memory long keeps getting 'u's.
Comments
Post a Comment