#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 Iteration | length | str[length] |
---|---|---|
Before While Loop | 0 | m |
After Iteration 1 | 1 | u |
After Iteration 2 | 2 | m |
After Iteration 3 | 3 | b |
After Iteration 4 | 4 | a |
After Iteration 5 | 5 | i |
After Iteration 6 | 6 | '' |
After Iteration 7 | 6 | Loop Terminated |
printf("\nLength of the String is : %d",length);
1 comment:
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
Post a Comment