0
What this code block does ?? c++
void fonc(char *str){ if(*str == '\0') return ; fonc (str+1); cout << str[0]; } It's was in a coding book. But I didn't get what it really does
1 ответ
+ 4
It prints passed string in reverse.
See if you pass to function "abcd"
str points at start of string.
func( str+1) calls function again recursively with advancing pointer that is from index 1 to end of string, until you pass empty string \0.
So first initial call is is ("abcd"), next is func("bcd")
func("cd")
func("d")
func("") This just returns. And to previous call func("d")
and prints str[0] => d
And back to previous call func("cd") Here str[0] is c so prints by cout<<str[0];
Similarly, recursively
prints b, of func("bcd") call. Next
prints a of func("abcd") call..
The recursion ends now..
Output you see is :
"dcba"
Hope it helps...