Skip to content
Home » C Programs » C Program to Find Length of String Without Using strlen Function

C Program to Find Length of String Without Using strlen Function

C Program to Find Length of String Without Using strlen Function. The strlen() method determines how long a string is. In the string. h header file, the strlen() method is defined. The null character ‘0’ is not counted. You can also find the length of a string without using the strlen function.

To find it, we construct a function. We read all of the characters in the string and increment the number by one if the character isn’t a null character. The counter equals the length of the string once the null character is discovered.

Strlen() Function in C

The strlen() function returns the length of a string. The strlen() function is defined in the header file string. h. The null character ‘0’ is not counted. In C, a string is an array of characters that ends with a null character (\0). 

The number of characters in a string is represented by the string length. In C, the strlen() function is used to determine the length of a string. 

Character in C Program

To store characters and letters, C employs the char type. However, the char type is an integer type because C stores integer numbers rather than characters. Char values in C are stored in 1 byte of memory and have a value range of -128 to 127 or 0 to 255.

While Loop in C Program

A while loop is also referred to as a pre-tested loop. A while loop, in general, allows a section of code to be executed multiple times depending on a boolean condition. It can be thought of as a looping if statement. The while loop is commonly used when the number of iterations is unknown in advance.

C Program to Find Length of String Without Using strlen Function

#include <stdio.h>
int main()
{
char a[1000];
int c = 0;
printf("Enter the element\n");
gets(a);
while (a[c] != '\0')
c++;
printf("Length of the element %d\n", c);
return 0;
}

Output:

Welcome to inlarn

Length of the element 

14