Home

A program to find the length of a string without using library function

C Programming Language 066

The following code shows how to write a program to find the length of a string without using library function.

Program

#include <stdio.h>

void main()
{
    char str[30];
    int i, len = 0;

    printf("Enter string: ");
    gets(str);

    for (i = 0; str[i] != '\0'; i++)
    {
        len++;
    }

    printf("Length of string = %d", len);

    printf("\n");
}

Result

Enter string: test-program
Length of string = 12


Last Updated on

Next Post: Rules vs Guidelines →

Comments