Help Me with string funcitons

I have a buffer which contains bunch of data. I want to take out the index at some desired string stored on which line number in that buffer. for ex

buffer A = {'0','1',0x0D,0x0A,0, '2','3',0x0D,0x0A,0, '4','5',0x0D,0x0A,0, '6','7',0x0D,0x0A,0}

I want to point the line location at which string "45\r\n" stored in this buffer

so the answer is 2.

here is my code.

but every time I am getting answer 0. Please help

int16 stringIndex(uint8 * S,uint8 * T)
{ uint16 i,l;

l=strlen(T);

for(i=0; * (S+i); i++) { if(strncmp(S+i,T,l)) return i; }

return -1;
}

void main(void
{

uint8 A[25];

sprintf( A,"01\r\n%c23\r\n%c45\r\n%c67\r\n%c,0,0,0,0");

stringIndexValue = stringIndex(A,"45\r\n");
}

Parents
  • uint8 A[25];
    
    sprintf( A,"01\r\n%c23\r\n%c45\r\n%c67\r\n%c,0,0,0,0");
    


    Why do you use sprintf()?

    That is a complicated way of inserting \0 characters in a string.

    for(i=0; * (S+i); i++) { if(strncmp(S+i,T,l)) return i; }
    


    You have a for loop that you want to exit if *(S+i) - which is nicer to write as S[i] - finds a string termination. Can you tell me how you expect to reach your "45\r\n" when you have two string terminations before that substring?

    Next thing - your goal is to return the value 2, enumerating the text lines 0, 1, 2, 3, ...
    But your loop is iterating characters. So if it did reach "45\r\n", it would not return 2. It would return 10.

    But didn't you catch this when you debugged your code? Exactly what did you find your program doing when you debugged it???

Reply
  • uint8 A[25];
    
    sprintf( A,"01\r\n%c23\r\n%c45\r\n%c67\r\n%c,0,0,0,0");
    


    Why do you use sprintf()?

    That is a complicated way of inserting \0 characters in a string.

    for(i=0; * (S+i); i++) { if(strncmp(S+i,T,l)) return i; }
    


    You have a for loop that you want to exit if *(S+i) - which is nicer to write as S[i] - finds a string termination. Can you tell me how you expect to reach your "45\r\n" when you have two string terminations before that substring?

    Next thing - your goal is to return the value 2, enumerating the text lines 0, 1, 2, 3, ...
    But your loop is iterating characters. So if it did reach "45\r\n", it would not return 2. It would return 10.

    But didn't you catch this when you debugged your code? Exactly what did you find your program doing when you debugged it???

Children
More questions in this forum