String Length in C Programming
String Length in C Programming
  • Post author:
  • Reading time:2 mins read
  • Post category:C Programs

C Program to Find the Length of a String

You can call strlen() function to find the length of an array as shown below:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[10]="Hello";
    printf("\n\n\tThe Length of the String is: %d\n\n\n",strlen(str));
    return 0;
}

Output

    The Length of the String is: 5
C Program to Find the Length of a String

Code Explanation

First, initialize a string or read it from the console.

Then call strlen() function and pass the c string to find the length of the c string.


String Length Without Using strlen in C

You can refer to the below c program to find the length of the string without using strlen function.

#include<stdio.h>
#include<string.h>
int main()
{
    char str[10]="Hello";
       int length=0;
    for (int i=0; str[i]!='\0'; ++i)
    {
        length++;
    }
    printf("\n\n\tString Length Without Using strlen: %d\n\n\n",length);
    return 0;
}
    String Length Without Using strlen: 5
String Length Without Using strlen in C

Code Explanation

At first, initialize a string or read the input from the user.

Initialize the length variable to zero. Iterate the for loop for each character of the string. Increment the length variable for each character.

Print the length variable.


What is string length in C programming?

Length of the string in c programming is number of characters in the string.

How do I find the length of a C style string?

You can use strlen() function or use the for loop to count the number of characters in the string.