This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I'm in my first programming class (C ) and we're on recursive functions. The project this week was to write a recursive function to count the amount of characters (including white spaces) in a C string and return that amount to the screen. I found the answer to the problem by Googling, but I don't really understand the solution. Here's the function definition:
int str_length(char s[]) {
//base case
if (s[0] == '\0')
return 0;
//recursive calls to str_length
else {
return 1 str_length(s 1);
}
I understand the base case and generally how recursion works, but I specifically don't understand how the recursive call str_length(s 1);
works. Could someone explain it to me? I'd really appreciate it!
Subreddit
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...