c program to find the length of a string without using strlen

This program finds the length of a string without using strlen() function. strlen() is a built in function available in c, which returns the length of a string. The program reads a string and stores it in the character array a[]. By default a character array stores a '/0' at the end, which marks the end of string. The while loop uses loop variable i, which is initially set to 0. At each iteration in the while loop the value of count is incremented. The loop continues until, the value of a[i] equals '/0'.

Program



#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int count=0,i=0;

char a[10];

printf("Enter the string");

scanf("%s",&a);

while(a[i]!='\0')

{

count++;i++;

}

printf("%d",i);

getch();

}


Output

Length of a string without using strlen
c program to find the length of a string without using strlen


No comments: