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 Without using Library Function

#include<stdio.h>
#include<conio.h>
void main()
{
 char str[100];
 int length;

 printf("\nEnter the String : ");
 gets(str);

 length = 0;  // Initial Length

 while(str[length]!='')
        length++;

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

Explanation of Program :

In the above program we have accepted the string from the user.
printf("\nEnter the String : ");
gets(str);
After that we have initialized the length variable with zero. “length” variable is used to keep track of the number of character accessed.
length = 0;  // Initial Length
Initially length is 0. Now we are accessing very first character. If it is equal to NULL then we are terminating the loop else we are incrementing the length.
while(str[length]!='')
        length++;

Dry Run :

Consider input string – “mumbai”.
While loop Iterationlengthstr[length]
Before While Loop0m
After Iteration 11u
After Iteration 22m
After Iteration 33b
After Iteration 44a
After Iteration 55i
After Iteration 66''
After Iteration 76Loop Terminated
Now last step is to print the length of the string -
printf("\nLength of the String is : %d",length);

1 comment:

Anonymous said...

Casino No Deposit Bonus Codes
Best Casino Bonus Codes ✓ Exclusive Casino Bonus 샌즈카지노 Codes ✓ New Online Casinos with Free Spins ✓ Play Hundreds of Slots for 인카지노 free. No 메리트 카지노 주소 Deposit Required. New