w3resource
C Programming Exercises

C Programming: Find the length of a string

C String: Exercise-2 with Solution

Write a program in C to find the length of a string without using library function.

C Programming: Find the length of a string

Sample Solution:

C Code:

#include <stdio.h>
#include <stdlib.h>

void main()
{
    char str[100]; /* Declares a string of size 100 */
    int l= 0;
	
       printf("\n\nFind the length of a string :\n");
       printf("---------------------------------\n"); 	
       printf("Input the string : ");
       fgets(str, sizeof str, stdin);
    while(str[l]!='\0')
    {
        l++;
    }
    printf("Length of the string is : %d\n\n", l-1);
}

Sample Output:

Find the length of a string :                                                                                 
---------------------------------                                                                             
Input the string : w3resource.com                                                                             
Length of the string is : 15 

Flowchart:

Flowchart: Find the length of a string.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to input a string and print it.
Next: Write a program in C to separate the individual characters from a string.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming