Message-ID: <39CCF175.275438D7@netzero.net> From: "Kenneth A. Strom" X-Mailer: Mozilla 4.75 [en] (Win95; U) X-Accept-Language: en,pdf MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: Extract a value from a string? References: <39CABE68 DOT D5AA584B AT netzero DOT net> <969595461 DOT 718378 AT shelley DOT paradise DOT net DOT nz> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Original-NNTP-Posting-Host: pppa75-resaleracine1-2r7004.saturn.bbn.com X-Original-Trace: 23 Sep 2000 14:06:52 EST, pppa75-resaleracine1-2r7004.saturn.bbn.com Organization: GBM Technologies Ltd Lines: 34 X-Authenticated-User: therelic Date: Sat, 23 Sep 2000 18:07:08 GMT NNTP-Posting-Host: 208.243.164.16 X-Trace: petpeeve.ziplink.net 969732428 208.243.164.16 (Sat, 23 Sep 2000 14:07:08 EDT) NNTP-Posting-Date: Sat, 23 Sep 2000 14:07:08 EDT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com If the string is... Capture = 0 Would it be better to strstr for = and then extract from the position of = + 1 into another string and then atoi? Does atoi handle whitespace? Or should I search for the first number digit (0-9) and extract from there to the end and atoi? Ken. therelic AT netzero DOT net bowman wrote: > Edmund Horner wrote in message > news:969595461 DOT 718378 AT shelley DOT paradise DOT net DOT nz... > > char *str; // this is the string. > > int n; > > n = atoi (str); // method 1. > > sscans (str, "%d", &n); // method 2. > > couple of minor points: > using strtol() instead of atoi allow you to test if the string > was actually a valid number, as well as allowing a base > to be specified. > > checking the return from sscanf() will verify that a numeric > string was actually found, converted, and placed in 'n' > > otherwise, atoi will happily return 0 on a bogus string and > sscanf() will leave n as garbage.