2020-09-14 12:11 PM
hi everyone
I'm trying to search for a certain char array(say char Temp[]) within a lager array in this case, char Rcv[500]. here is my code
int scanRcv(char *Temp){
int ii=0;
for(int i=0;i<RcvLength;i++){
while(Rcv[i]==Temp[ii]){
ii++;
if(ii==strlen(Temp))
{
return 1;}
else
{return 0;}
ii=0;
}
}
but I don't know why it doesn't work and does not return 1, though I think the code works....
I also tried this code too but it didn't work either
int scanRcv(char *Temp){
if(strstr(Rcv,Temp)!=0)
{
return 1;
}
else
{
return 0;
}
}
please help
2020-09-14 01:18 PM
>>but I don't know why it doesn't work and does not return 1, though I think the code works....
Clearly it doesn't work. You reset the index at every iteration of the inner loop. You exit immediately on the first match/mismatch.
Get the indentation presented more clearly.
Get the logic worked out.
Make some assorted test cases, and show how the function gets used in normal operation, and that it recognizes the strings.
2020-09-14 09:16 PM
dear clive1
I just reset the index after I find the entire match (i.e. ii==strlen(Temp)). what's wrong with that?
2020-09-15 02:46 AM
As strstr() return a pointer, you should test for a null pointer in case string is not found.
e.g. if(strstr(Rcv,Temp)!=NULL) ....