Find Length of String


This operation is used to count number of characters in the string.
In order to count number of characters, we have to start counting from the first character in the string which is at index 0 in the array. The process of counting characters is repeated until NULL character is encountered in the string, because NULL character indicates end of the string.

T
E
S
T
\0
S1[0]
S1[1]
S1[2]
S1[3]
S1[4]

Algorithm to Find Length of String


Step 1: Length = 0
Step 2: Repeat step 3 while S1 [Length] ≠ NULL
Step 3: Length = Length + 1
Step 4: Return Length
     

Program to Find Length of String


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s1;
int l;
int slen(char *s1);
clrscr();
puts("Enter string:");
gets(s1);
l=slen(s1);
printf("Length of String is %d",l);
getch();
}
int slen(char *s1)
{
int length=0;
while(s1[length]!='\0')
{
length=length+1;
}
return length;
}

Download Projects


Download Programs