Wednesday, 15 September 2010

string - C : strchr pointer value doesn't change -



string - C : strchr pointer value doesn't change -

i'm trying recursively search substring in string using c program. wrote next piece of code. issue i'm facing that, ptr value, though prints right value(using puts in origin of while), while usage value not changed! uses previous ptr value. found out using gdb. couldn't figure out cause of this. kindly guide me solve issue. in advance.

void main() { char buf[10]="hello",*ptr; char findc[10]="lo"; int len,i,lenf,k,l,flag=0; lenf=strlen(findc); l=0,k=1; ptr=strchr(buf,findc[l]); while(ptr!=null) { puts(ptr); l++; for(i=l;i<(lenf);i++,k++) { if(ptr[k] != findc[i]) { flag=1; break; } } if(flag==1) { l=0;k=1; ptr=strchr((ptr+1),findc[l]); if(ptr==null) { puts("string not found"); break; } } else { puts("string found"); break; } } }

it simple mistake!

we'll have reset flag variable in origin of while loop. solve issue.

thanks!

c string substring strchr

No comments:

Post a Comment