Thread: strlen function help

  1. #16
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by quzah View Post
    Code:
    int mystrlen( char *s )
    {
        char *p = s;
        while( p && *p )
            p++;
        return p - s;
    }
    Quzah.
    hmm this looks technical :S LOL
    im doing a little on recursion if you could explain this genius of your it would be helpful

    im going to try this out and try and understand it and hopefully my thoughts can match your description

    thanks

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int mystrlen( char *s )
    {
        char *p = s; /* make p point to s */
        while( p && *p ) /* while p is not null, and *p is not \0 */
            p++; /* increment p */
        return p - s; /* return the difference between p and s */
    }
    Pointers are values. If you have a pointer pointing to the start of an array (s) and you have a pointer pointing further along in that same array (p), then the difference between (p) and (s) is how far along the array you are.
    Code:
    p = s;
    p++;
    
    if( p - s == 1 )
        printf( "p is 1 element along the array" );
    So, if the loop fails right off, then p and s will be pointing to the same. So, p - s will be zero. If we advance P one spot, then p - s will be 1. And so on.

    As for the recursive function:
    Code:
    check to see if s is NULL, and not '\0'
        if it's not
            return 1 (counting that it's a valid non-nul character) + whatever further recursive calls return
    So, if we have a string that is 2 characters long, not counting the nul:
    Code:
    if s and *s
        return 1 (the first character) + (recursive call, points to the next character)
            if s and * s
                return 1 + recursive call now will be \0
                if s and *s ..fails because it's now \0, so it returns 0
    So, 0 + 1 + 1 = 2.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    As for the recursive function:
    Code:
    check to see if s is NULL, and not '\0'
        if it's not
            return 1 (counting that it's a valid non-nul character) + whatever further recursive calls return
    So, if we have a string that is 2 characters long, not counting the nul:
    Code:
    if s and *s
        return 1 (the first character) + (recursive call, points to the next character)
            if s and * s
                return 1 + recursive call now will be \0
                if s and *s ..fails because it's now \0, so it returns 0
    So, 0 + 1 + 1 = 2.


    Quzah.
    ok well i understood the first part but the recursive (above) part is just playing with my mind a little but im slowly making sense of it

    so far ive understood that whilst the end of the string (\0) is not achieved then you return 1 and something else. what i need to know is what is the difference between 'NULL' and '\0'?
    and
    how do you put the recursive function into code (what fills the (...)'s after return 1?

    your such a help Quzah thanks loads

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was just illustrating that it was recursing there. That wouldn't actually be in the function itself. Here is the difference between NULL and a nul character:
    Code:
    char *p = NULL;
    char c = '\0';
    The pointer p is set to "point nowhere". The character c contains a nul character, meaning the value is zero. If we do this:
    Code:
    p = &c;
    Then the pointer p is actually pointing some place (it's pointing at the character c, whose value is nul). So, if we did:
    Code:
    if( p == NULL )
        printf( "this won\'t print because the pointer actually points some place" );
    else
    if( *p == '\0' )
        printf( "this will print because the value of c is nul" );
    else
        printf( "this won\'t print because the value of c is nul" );
    Edit: The nul character (or terminator) is generally used to indicate the end of strings.


    Quzah.
    Last edited by quzah; 03-24-2010 at 04:51 PM.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ow ok cool thanks you cleared a few dark clouds now
    thanks time to work on this tomorrow now

  6. #21
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    @Quzah.
    Thank you so much for your through explanation, I did learnt a lots from it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM