Solved programming problem

How to Find the length of a String with Pointer in C.

Source code

#include<stdio.h>

int main()

{

    int i=0,len=0;

    char str[100];

    char *ptr;  ///Declaring  a character type pointer.

    printf("Please Enter A Massage: ");

    gets(str);

    ptr=str; /// pointer that points to string

    ///finding the length

    while(*(ptr+i) != '')

    {

        len++;

        i++;

    }

    printf("
String Length: %d
",len);

    return 0;

}

Output

Please Enter A Massage: apple

String Length: 5

 

Join the discussion

Add your comment