Using Pointers to Find the Length of a String - C String

C examples for String:char array

Description

Using Pointers to Find the Length of a String

Demo Code

#include <stdio.h>

int stringLength (const char *string){
    const char *cptr = string;

    while ( *cptr )
        ++cptr;/*from  ww w.ja  v a  2s.  co m*/
    return cptr - string;
}

int main (void)
{
    int stringLength (const char *string);

    printf("%i  ", stringLength ("test test test test"));
    printf("%i  ", stringLength (""));
    printf("%i  ", stringLength ("complete"));

    return 0;
}

Result


Related Tutorials