Where is mistake in this script ?
#include <string.h>
#include <stdio.h>

      char *string = "<aa>RRRR|<aa>SSS|";
      char *cmp = "<aa>";
      char *p1;
        int nlen;

      main()
      {
        while ( p1 = strstr(string, "<aa>")  ) {
        nlen = 0;
        p1 = p1 + 4;
        while ( ( *p1 != '|' ) && ( *p1 != '\0') ) { p1 = p1 + 1; nlen++; };
        p1 = p1 - nlen;
        string = p1 + 1;
        char rr[200];
        strncpy(rr,p1,nlen);
        printf("%s\n", rr);
        }
      }


Wrong answer:
RRRR
SSSR


Where is mistake in this script ?
===

#include <string.h>
#include <stdio.h>

      char *string = "<aa>RRRR|<aa>SSS|";
      char *cmp = "<aa>";
      char *p1;
        int nlen;

      main()
      {
        while ( p1 = strstr(string, "<aa>")  ) {
        nlen = 0;
        p1 = p1 + 4;
        while ( ( *p1 != '|' ) && ( *p1 != '\0') ) { p1 = p1 + 1; nlen++; };
        p1 = p1 - nlen;
        string = p1 + 1;
        char rr[200];
        strncpy(rr,p1,nlen);
        printf("%s\n", rr);
        }
      }


Wrong answer:
RRRR
SSSR

