New way to learn C programming

C Program to find the length of sting

This is a user defined program to prints the length of string, let’s take an example "pTutorial" its length is nine. You can also use strlen function of string.h library to calculate the length of string.

Example of calculate the length of string

//calculate the length of the string
// without any inbuilt function
#include <stdio.h>
#include <conio.h>
void main()
    char a[1000],i;
    printf("Enter any string to find its length: ");
    for(i=0; a[i]!='\0'; ++i);
    printf("Length of  the string is: %d",i);

Download Length Of String C Program

In this program we are using a character type array which store the string (which is given by user) with the help of for loop, we are reading each character and count the length until it is found null.

When first time for loop will execute, then value of i=0 it will check the condition of for loop the a[0] is null or not if it is not null then just increase the value of i counter i.e. '1'

Now check the condition again and if it is true then again increase the value of the "i" counter. It will continuously increase the value of i Counter, until the condition is false. When condition will false it would exit from the for loop and print the value of the counter i.

Output of program

C program to count string length | ptutoriala

C program to calculate length of string using function

//C Program for length of string
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
   char str[70];
   int string_len;
   printf("Enter a string to calculate the length\n");
   string_len = strlen(str);
   printf("Length of string = %d\n",string_len);

Download String length program

In this example, str is a character type array that stores the entered string string_len is an integer variable that stores the length of the string.

gets function is used to take string by the user and the strlen function returns the number of characters in the given string.

See Also

Jquery example pdf   |   Php echo   |   Php do while loop


Crafty men condemn studies, simple men admire them, and wise use them.