Program to find length of string in C: To count the total number of characters present in the given string. We can find the length of the string in 2 ways :
1. Using String length function
2. Without using the string length function
In this program, we find length of the string without using string function.
String: String always ends with the null character.
Using this important point we can find the length of a string without string function.

Algorithm length of string in c

  1. Declare variable. count=0
  2. Take input string from user. Str
  3. Traverse string Str
    Increment counter variable. count++
    If Str == ‘\0’
    break
  4. Print length of string. count

Program to find length of string in C

#include <stdio.h>
#include<conio.h>
int main()
{
char str[30];
int i;

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

for(i = 0; str[i] != '
#include <stdio.h>
#include<conio.h>
int main()
{
char str[30];
int i;

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

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

printf("Length of string %s: %d", str,i);
return 0;
}
'; ++i); printf("Length of string %s: %d", str,i); return 0; }

Output:

Program to find length of string in C

Write A Comment