You're seeing garbage at the end of rbuffer because you're not terminating the string properly. You do need the i++ there to index past the closing tag.
Look at this version to see the diagnostics as I had envisioned them:
Code:
#include <stdio.h>
#include <string.h>
int htmlparser(char buffer[])
{
int i;
int j = 0;
int m = 0;
int len = strlen(buffer);
char rbuffer[len+1];
for(i = 0; i < len+1; i++) rbuffer[i] = 'X' ;
rbuffer[len] = 0 ;
for(i = 0; i < len; i++)
{
if(buffer[i] == '<')
{
while(buffer[i] != '>')
{
i++;
}
//if i add a i++ here i get 3 jibberish characters at end of string
i++ ;
}
rbuffer[j] = buffer[i];
printf("i=%d, j=%d\n", i,j);
printf("buffer = %s\n",&buffer[i]);
printf("rbuffer = %s\n",rbuffer);// prints correctly
j++;
}
i++;
printf("\n\n%s", rbuffer); // prints the number 2
return 0 ;
}
int main()
{
//char *ptr;
char buf[] = "<fdfdfdfdffdfdfdf>ignore all previous words <> <tttdfeerrf>done.";
//ptr = buf;
htmlparser(buf);
return 0;
}
Glad you're getting it!
Todd