# Thread: Recursion Function - OverStacked (Help!)

1. ## Recursion Function - OverStacked (Help!)

I have to implement a recursive function. But I got stuck there.

The function I write is like that:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int strlenRecursive(char* str) {

if (str == '\0')
return 0;

else
return strlenRecursive(str-1);
}

int main() {

char buffer[256];

printf("Enter a string and I will tell you the length (CTRL-Z to quit)\n");
while (scanf("%s", buffer) != EOF) {
printf("The Length of '%s' is %d\n", buffer, strlenRecursive(buffer));
}

fflush(stdin);
getchar();
}

I have no idea on how to solve it which took me nearly one week!.

Thanks

2. What exactly is your function supposed to do? You realize that you're passing it a pointer to a string, and then going BACKWARDS from the START of it, right? You did know that, right?

Ok, you have no idea what you're doing here, so I'll help. In a nutshell, you want to keep track of some number and return that as your total length.
Code:
```int recursivestrlen( char *s, int x )
{
if( !s+x ) return 0;
return recursivestrlen( s, x+1 );
}```
That should do it. Call the function:

recursivestrlen( myString, 0 );

Quzah.

3. I would bet money that you are getting the stack overflow because you have a function calling itself the same number of times as the length of the string (if the code worked) and each time new variables are created, etc, solution is to use a loop in a function so that it is only called once, enjoy:

#include <stdio.h>
#include <stdlib.h>

int strlenRecursive(char *str) {
int counter=-1;
while(str[++counter]!='\0');
return counter;
}

int main() {
char buffer[256];

do{
printf("Enter a string and I will tell you the length (Q to quit)\n");
gets(buffer);
if(buffer[0]!='Q' || buffer[1]!='\0')
printf("The Length of '%s' is %d\n\n", buffer, strlenRecursive(buffer));
} while(buffer[0]!='Q' || buffer[1]!='\0');
return 1;
}

4. > I would bet money that you are getting the stack overflow
> because you have a function calling itself the same number of
> times as the length of the string (if the code worked) and each
> time new variables are created, etc

1) The function doesn't work.
2) Here is what happens:
Code:
```func( stringAddy )
...and on and on and on...```
He isn't decrementing a counter. He's decrementing the pointer's value. As such, he keeps making the value point some place else in memory. God only knows where he ends up pointing.

3) No variables are actually declared in his function. Therefore, it takes him a long long time to actually overflow stack space this way (a few seconds at most )

Anyway, you're close to being right. He eventually runs out of stack space, or more likely, tries accessing some point in memory that he doesn't have access to.

Furthermore, do you know what recursion is? Your example is NOT recursive, which is the whole point of his example/lesson.

Quzah.

5. yeah probly right, but either way having a function call its self for an unknown number of times is a bad idea in my opinion.