Re: parsing a string into substring and integers



Peter Nilsson wrote:
Ian Collins wrote:
monkeys paw wrote:
Hello all, what is the easiest way to parse the following
string into components?


char state[3];
int number;
int calendar_year;

int main()
{

char *s = "CA200617456";

}

What would need to happen after the parse is this:

state= "CA"
number=17456
calendar_year=2006

If the input format is fixed, use strncpy, passing the start position
and size of each field.

If you want to convert the number to integer, strtol and friends can help.

Or just let sscanf do all the work for you...

int r = sscanf(s, "%2s%4d%4d", state, &number, &calendar_year);


Thank you for doing his homework.

--
Regards,
Stan Milam
=============================================================
Charter Member of The Society for Mediocre Guitar Playing on
Expensive Instruments, Ltd.
=============================================================
.



Relevant Pages

  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Gcc compatible header file
    ... A string collection is a table of zero terminated strings that will grow ... typedef struct _StringCollection StringCollection; ... int; ... bool; ...
    (comp.lang.c)
  • Kernighan and Pikes "Beautiful" Code
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • Re: FTP CD command
    ... public const int GENERIC_WRITE = 0x40000000; ... string lpszProxyName, ... public static extern IntPtr InternetConnect ( ... public static extern bool FtpGetCurrentDirectory ( ...
    (microsoft.public.dotnet.languages.vb)