C Program to Find the Length of a String

@codevarsity
 · 
2.1k views
 · 

In this sample C program, you'll learn to find the length of a string without using strlen() function.

Code

#include <stdio.h>
int main()
{
    char s[1000], 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

Comments