String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • magix

    String

    Hi,

    If I have string like below:
    InputString= "@123456^BI LL GATES^APR-2011 ?"

    where @ - Start Character
    ^ - Separator
    ? - End Character

    and it contains ID, NAME, and DATE

    Pseudocode:

    char * ID;
    char * NAME;
    char * DATE;

    if ( (start char is @) AND (End Char is ?) AND (There are two ^ in string))
    Then
    { //This is valid string

    strncpy(ID, &InputString[1], 6); // ID has fix length
    ID[6] = '\0'; // terminate the string

    strncpy(NAME, &InputString[8], (Length of variable NAME Length) );
    // NAME has variable length, but it is between two ^
    NAME[strlen(NAME)] = '\0'; // terminate the string

    strncpy(DATE, &InputString[position after the 2nd ^], (Length of
    date between 2nd ^ and ?)); // Date is between 2nd ^ and ?
    DATE[strlen(DATE)] = '\0'; // terminate the string
    }

    can help to transform into code (some string functions..i'm not too sure to
    achieve and finding the length /position) ? Many thanks.

    Regards.


  • Peter Nilsson

    #2
    Re: String

    "magix" <ma...@asia.com wrote:
    Hi,
    >
    If I have string like below:
    InputString= "@123456^BI LL GATES^APR-2011        ?"
    >
    where @ - Start Character
    [Note that @ need not be a character in the basic
    execution character set.]
              ^  - Separator
               ? - End Character
    >
    and it contains ID, NAME, and DATE
    >
    Pseudocode:
    >
     char * ID;
     char * NAME;
     char * DATE;
    >
    if ( (start char is @) AND (End Char is ?) AND (There are two ^ in string))
    Then
    {    //This is valid string
    >
            strncpy(ID, &InputString[1], 6);    // ID has fix length
            ID[6] = '\0';    // terminate the string
    >
            strncpy(NAME, &InputString[8], (Length of variable NAME Length) );
    // NAME has variable length, but it is between two ^
            NAME[strlen(NAME)] = '\0'; // terminate the string
    >
            strncpy(DATE, &InputString[position after the 2nd ^], (Length of
    date between 2nd ^ and ?));  // Date is between 2nd ^ and ?
            DATE[strlen(DATE)] = '\0'; // terminate the string
    >
    }
    >
    can help to transform into code (some string functions..i'm not too sure to
    achieve and finding the length /position) ? Many thanks.
    Untested...

    const char *pc1, *pc2, *pcm;

    if ( InputString[0] == '@'
    && (pc1 = strchr(InputStr ing + 1, '^')) != 0
    && pc1 - &InputString[1] == 6
    && (pc2 = strchr(++pc1, '^')) != 0
    && (pcm = strchr(++pc2, '?')) != 0
    && pcm[1] == 0)
    {
    strncpy(ID, InputString + 1, 6);
    ID[6] = 0;

    strncpy(NAME, pc1, pc2 - pc1 - 1);
    NAME[pc2 - pc1 - 1] = 0;

    strncpy(DATE, pc2, pcm - pc2);
    DATE[pcm - pc2] = 0;
    }

    --
    Peter

    Comment

    • Eric Sosman

      #3
      Re: String

      magix wrote:
      Hi,
      >
      If I have string like below:
      InputString= "@123456^BI LL GATES^APR-2011 ?"
      >
      where @ - Start Character
      ^ - Separator
      ? - End Character
      >
      and it contains ID, NAME, and DATE
      >
      Pseudocode:
      >
      char * ID;
      char * NAME;
      char * DATE;
      >
      if ( (start char is @) AND (End Char is ?) AND (There are two ^ in string))
      Then
      { //This is valid string
      >
      strncpy(ID, &InputString[1], 6); // ID has fix length
      ID[6] = '\0'; // terminate the string
      >
      strncpy(NAME, &InputString[8], (Length of variable NAME Length) );
      // NAME has variable length, but it is between two ^
      NAME[strlen(NAME)] = '\0'; // terminate the string
      >
      strncpy(DATE, &InputString[position after the 2nd ^], (Length of
      date between 2nd ^ and ?)); // Date is between 2nd ^ and ?
      DATE[strlen(DATE)] = '\0'; // terminate the string
      }
      >
      can help to transform into code (some string functions..i'm not too sure to
      achieve and finding the length /position) ? Many thanks.
      There's more than one way to do this, but here's one
      approach with the advantage of simplicity. I'll assume
      the indicated fixed maximum sizes for NAME and DATE; other
      approaches could cope with variable sizes.

      char ID[6+1];
      char NAME[40+1];
      char DATE[8+1];
      int len;

      /* Grab the three fields and get the total length */
      if (sscanf(InputSt ring,
      "@%6[^^]^%40[^^]^%8[^^?]?%n",
      ID, NAME, DATE, &len) != 3)
      return BADSTRING;

      /* Expected length for ID? */
      if (strlen(ID) != 6)
      return BADSTRING;

      /* Optional: Any garbage after the end? */
      if (InputString[len] != '\0')
      return BADSTRING;

      /* All's well */
      return GOODSTRING;

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      Working...