About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Wednesday, September 4, 2013

C Program to Find Length of String with using user defined function

#include<stdio.h>
#include<conio.h>

int FindLength(char str[]);      // Prototype Declaration

void main()
{
 char str[100];
 int length;
 printf("\nEnter the String : ");
 gets(str);

 length = FindLength(str);

 printf("\nLength of the String is : %d",length);
 getch();
}

//Writing function

int FindLength(char str[])
{
 int len = 0;
 while(str[len]!='')
    len++;
 return(len);
}

Explanation of the program –

after accepting the string from the user we are passing the complete array to the function.
length = FindLength(str);
Now inside the function we are executing a while loop which will calculate the length of the string.
int FindLength(char str[])
{
 int len = 0;
 while(str[len]!='')
    len++;
 return(len);
}