String

Discussion in 'C Programming' started by magix, May 21, 2008.

  1. magix

    magix Guest

    Hi,

    If I have string like below:
    InputString= "@123456^BILL 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.
    magix, May 21, 2008
    #1
    1. Advertising

  2. "magix" <> wrote:
    > Hi,
    >
    > If I have string like below:
    > InputString= "@123456^BILL 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(InputString + 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
    Peter Nilsson, May 21, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    733
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,591
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    499
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,906
    John C. Bollinger
    Nov 19, 2004
  5. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,801
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
Loading...

Share This Page