C Program to Find Length of a String

How to write a C program to find string length using strlen and without using strlen function. We can calculate the string length in C Programming in multiple ways. And they are using Inbuilt string function strlen and without using library function strlen in C

C strlen Function

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

strlen(<string name>)

C 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 Length;
 
  printf("\n Please Enter any String \n");
  gets (Str);
 
  Length = strlen(Str);
 
  printf("Length of a String = %d\n", Length);
 
  return 0;
}

C string length output

 Please Enter any String 
Tutorial Gateway
Length of a String = 16

Within this c strlen example program, We declared character array variable of size 100 and names 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 c strlen,

Length = strlen(Str);

We are finding the length of a string using the C 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 of Your String = %d \n", Length);

From the above screenshot, you can observe that we entered 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 in C.

#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 C 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, 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 c 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 in C.

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

We haven’t done anything special in this C Program to Find Length of a String. We just changed the while loop in the above c strlen code to for loop. If you have any queries regards to the for loop. Please refer For Loop article here: For Loop.

C string length output

 Please Enter any String 
Tutorial
Length of Your String = 8