Home / Programs / C Program to Find the Length of a String
Programming Example

C Program to Find the Length of a String

👁 865 Views
💻 Practical Program
📘 Step by Step Learning
In this article, you'll learn to find the length of a string without using strlen() function.

Program Code

#include <stdio.h>
int main()
{
    char s[1000];
    int i;

    printf("Enter a string: ");
    scanf("%s", s);

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

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

Output

Enter a string: Programiz
Length of string: 9

Explanation

This program asks user to enter a string and computes the length of string manually using for loop.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.