/********
to write :a prog that calculates the len of str
-take as a prmtr a const char ptr
-use ptr arithmetic(++)
-use while loop
-subtract two ptrs
-return int
*********/


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

int getLen(const char *ptr);

int main() {
    char str[] = "Hello";
    char *ptr2 = &str[0];

    int length = getLen(ptr2);
    printf("%d", length);

    return 0;
}

int getLen(const char *ptr){
    const char *start = ptr; 
    //here: store starting adr (X value:*ptr)

    while(*ptr != '\0')
        ptr++;  

    int len = ptr - start;
    return len;

}


Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: