program to calculate the length of the string by pointer using while loop
In this program ,we will now calculate the length of the string by pointer using while loop by taking taking string as a input from the user.
pointer :- pointer is a variable which contain address of another variable.
- gets() is used to accept string with spaces.
- the base address is stored in the variable i . Once the base address is obtained in i , *i would yield the value at this address .
- Inside while loop we are going to count single letter and incrementing pointer further till we get null character.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
char *i,count=0;
i=str;
printf("enter the string:-");
gets(str);
while(*i!='\0'){
count++;
i++;
}
printf("\n the length of the string is %d:-",count);
getch();
}
output:-
For more c-programming:-

No comments:
Post a Comment