String

M

magix

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.
 
P

Peter Nilsson

magix said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top