/*Calculate string length without using predefine function in C language*/
#include<stdio.h>
void main()
{
char str[100]="I am the best",*ptr;
ptr=str ;
int count = 0,i;
for(i=0;ptr[i]!='\0';i++)//can use also--while(*(ptr+count))
{
count++;
}
printf("\nThe length of the string is %d.",count);
}//End of main
:-Programming Error
carefully use ++ oprator and pointer.
loop should be run till null
:-Details of Programme
*ptr = its a pointer which is use for reference ;
count=0 count shuld initialize here with 0 because if its not then compiler assign garbage value to count and output will be diferent.
No comments:
Post a Comment