Help: Recursive String Length Function

From: Andy (anaylor_at_nc.rr.com)
Date: 01/25/04


Date: 24 Jan 2004 18:01:07 -0800

Hi,

Problem is to write a recursive function w/out strlen() to determine
the length of a c-style string array.

Can't Use pointers. Only arrays.

Problem is when I run it, it generates a segmentation fault.

Debuggin I see it walk the array to \0 but then it doesn't come
back/return.

Is this a problem with the recursion or the way I'm calling the
function?
Any ideas would be helpful.

Here is the function:

// Arguments:
// buffer - a c-style string to recursively measure the length of
// Returns:
// int - the length of the string passed in
// Side effects:
// none
int recursivestringlength(const char buffer[]){
if (buffer=='\0')
 return 0;
        else
        return(1+recursivestringlength(buffer+1));
      
}

Function Call:
char s[80]="hello";
int job = recursivestringlength(&s[1]);

Thanks,
Andy...



Relevant Pages

  • Re: A simple question about opreator.
    ... I want to write a recursion function that returns the maximum among the ... first n elemetns of an array, using at most "lgn" recursive calls. ... int n) ...
    (comp.lang.java.programmer)
  • Re: Programming challenge
    ... to be doing a variable number of multiplications. ... int fact=1; ... are the looping constructs, goto, and recursion. ... but we would then have the problem of setting up the array.) ...
    (comp.lang.c)
  • Re: [PHP] unset in foreach breaks recrusion
    ... but on next recursion when it's not empty - do nothing:) ... Though I found how to cut a few seconds (on very big array), ... (although I've been working with PHP for at least 3 years), ... entry from each parent it can get to and it only can get to few, ...
    (php.general)
  • Re: [PHP] unset in foreach breaks recrusion
    ... $sorted is empty, if it is -> run the foreach and return, ... but on next recursion when it's not empty - do nothing:) ... Though I found how to cut a few seconds (on very big array), ... Now, about your other suggestion, yes, it will work without the reference. ...
    (php.general)
  • Re: counting subsets of S so that sum(S_n) = N
    ... and make my algorithm generate only unique subsets? ... and the array of boolean at one step of the algorithm is ... implementation without needing recursion. ... subsequence in W, process it and return. ...
    (comp.programming)