EmbLogic's Blog

loop counting 1 extra…why?

char *a,*b; int c,n,i;

printf(“enter the word to be find: “);

a=malloc(10*sizeof(char));

b=malloc(100*sizeof(char));

printf(“Address of b %d”,b);

fgets(a,10,stdin); puts(a);

i=0;

printf(“len of the str:%d \n”,strlen(a));

//for(i=0;*(a+i)!=”;i++)

while(*(a+i)!=”)

{

printf(“length= %d\n”,i);

printf(“length======== %c\n”,*(a+i));

i++;

}

One Response to loop counting 1 extra…why?

  1. Pavan Patil says:

    hii
    because at the end of every string complier always puts ” (NULL) character thts why ur loop count is always string+1

Leave a Reply to Pavan Patil Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>