C Program to Find Length of a String

How to write a C program to find string length using strlen and without using the function. We can calculate the string length either by using the Inbuilt string function or without using the strlen library function.

C strlen Function

strlen function is a built-in strlen string function in C Programming, and it is useful to find the string length. Syntax of the strlen function is:

strlen(<string name>)

The strlen function will count the number of characters in a string until it reaches the null value (\0) and returns that integer value as output.

C Program to Find the Length of a String

This program allows the user to enter any string or character array. Next, it will find the length of a string using a built-in string function called strlen in C programming.

#include <stdio.h>
#include <string.h>
 
int main()
{
  char Str[100];
  int lg;
 
  printf("\n Please Enter any String \n");
  gets (Str);
 
  lg = strlen(Str);
 
  printf("Length = %d\n", lg);
 
  return 0;
}
 Please Enter any String 
Tutorial Gateway
Length = 16

Within this example program, We declared a character array variable of size 100 and named as Str, and we also declared one integer variable, Length.

The below printf statements asks the user to enter any word that they would like to find the length.

printf("\n Please Enter any String \n");

The below statement in this program will assign the user entered string to the Str variable.

gets (Str);

In the next line, we have strlen,

lg = strlen(Str);

We are finding the length of a string using the strlen function and also assigning the value to the Length variable.

Below C Programming printf statement will print the value inside the Length (means string length)

printf(" Length = %d \n", lg);

From the above screenshot, you can observe that we entered the string “Tutorial Gateway”, and the length of a string = 16 (including the black space in between 2 words)

C Program to Find Length of a String without Using strlen

We can achieve the same in multiple ways, but we will discuss two approaches called For Loop and While Loop

C Program to Find Length of a String Using While Loop

This program allows the user to enter any string or character array, and it will find the length of a string without using a strlen function.

#include <stdio.h>
#include <string.h>
 
int main()
{
  char Str[100];
  int Length = 0;
 
  printf("\n Please Enter any String \n");
  gets(Str);
 
  while (Str[Length] != '\0')
   {   
     Length++;
   }
   
  printf("\nLength of a String = %d", Length);
  return 0;
}
Length of a String Without Using strlen

If you look at the While loop in this program for string length,

while (Str[Length] != '\0')
 {   
     Length++;
 }

Every string ends with the null character “\0”. So the condition inside the while loop (Str[Length] != ‘\0’) will fail when the string reaches the “\0” position.

Inside the while loop, we are using the Increment Operator to increase the value of Length by 1.

Within this example, we haven’t used c strlen. The user Entered the String “you”
Initially, the length value will be 0.

First Iteration
Initially, the length value will be 0.
(Str[0] != ‘\0’) condition is TRUE because Str[0] = y
So, the length incremented to 1

Second Iteration
Now, the Value of Length is 1
(Str[1] != ‘\0’) condition is TRUE because Str[1] = o. So, the length incremented by 1

Third Iteration
The Value of Length is 2
(Str[2] != ‘\0’) condition is TRUE because Str[2] = u. So, the length incremented by 1

Fourth Iteration
Length = 3, the While condition (Str[3] != ‘\0’) is FALSE because Str[3] = \0. So, the loop in this strlen example will terminate.

The final printf statement will print the value inside the Length variable (means length of a string)

printf("\n Length of the String is : %d", Length);

Final output is
Length of s String = 3

C Program to Find Length of a String Using For Loop

This program allows the user to enter any string or character array, and it will find the length of a string without using strlen function.

#include <stdio.h>
#include <string.h>
 
int main()
{
  char Str[100];
  int i;
 
  printf("\n Please Enter any Sentence \n");
  gets(Str);
 
  for (i = 0; Str[i]!='\0'; i++);
  
  printf("Length = %d\n", i);
  return 0;
}

We haven’t done anything special in this example. We just changed the while loop in the above code to for loop. If you have any queries regards to the for loop. Please refer For Loop article here: For Loop.

 Please Enter any Sentence 
Tutorial
Length = 8