C Program to print the length of the string without using built-in function

One of the easiest ways to fine the length of the string in C is by using built-in function strlen.

Using below code, we can find the length of the string without using any built-in functions.

/* C Program to print the length of the string without using built-in function*/
#include <stdio.h>
int main()
{
    /*Take a sample string*/
    char *str = "Hello";
    int counter = 0; 
    
    /*Traverse till end of the string*/
    while(str[counter] != '\0')
    {
        counter++;
    }
    
    printf("\r\nLength of the string = %d", counter);

    return 0;
}

Output:

Length of the string = 5

Leave a Comment