Index « Previous Next »

Question

Write a program that prompts the user to input a string and display the length of string.

Source Code

#include <stdio.h>

int main()
{
    char str[80];
    int l;

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

    /* Find the length of the string */
    for (l = 0; str[l] != '\0'; l++);

    /* Display the length of string */
    printf("The length of string is: %d", l);

    return 0;
}

Output

Enter a string: C programming
The length of string is: 13