Too obfuscated?

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))

/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */

void DoStuff( const char *str )
{
const char *cp;

for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
}

Perhaps

void DoStuff( const char *str )
{
const char *cp;

cp=str;
while( cp ) {
if( cp == '.' )
cp++;
/* do things with atoi(cp) */
cp=strchr( cp, '.' );
}
}

would be better? Any even better suggestions?
 
N

nrk

Christopher said:
(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))

/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */

void DoStuff( const char *str )
{
const char *cp;

for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
}

Perhaps

void DoStuff( const char *str )
{
const char *cp;

cp=str;
while( cp ) {
if( cp == '.' )
cp++;
/* do things with atoi(cp) */
cp=strchr( cp, '.' );
}
}

would be better? Any even better suggestions?

How about:

void do_stuff(const char *str) {
char *cptr;

while ( *str ) {
long l = strtol(str, &cptr, 0); /* set base to suit your needs */
/* do stuff with l */
/* replace with *cptr == '.' if needed below */
str = *cptr ? cptr + 1 : cptr;
}
}

You do know that strtol is preferred over atoi, right? :)

-nrk.
 
J

Jarno A Wuolijoki

(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))

/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */

void DoStuff( const char *str )
{
const char *cp;

for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
Any even better suggestions?

I'd keep it simple:

if (str) {
const char *cp=str;
for (;;) {
/* do things with atoi(cp) */
cp=strchr(cp, '.');
if (!cp) break;
cp++;
}
}

None of the codes process stuff after the last '.' yet the regexp
indicates there is some, btw.
 
P

Peter Shaggy Haywood

Groovy hepcat Christopher Benson-Manica was jivin' on Mon, 2 Feb 2004
15:44:03 +0000 (UTC) in comp.lang.c.
Too obfuscated?'s a cool scene! Dig it!
(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))

/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */

void DoStuff( const char *str )
{
const char *cp;

for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
}

Ick!
Perhaps

void DoStuff( const char *str )
{
const char *cp;

cp=str;
while( cp ) {
if( cp == '.' )
^
You're missing a dereference here.
cp++;
/* do things with atoi(cp) */
cp=strchr( cp, '.' );
}
}

would be better? Any even better suggestions?

That's better, but still not great. You're effectively testing the
same value twice (strchr(cp, '.') followed by if(*cp == '.') in the
next iteration). A better approach would be to call strchr() in the
controlling expression of the loop, like so:

void DoStuff(const char *str)
{
while(NULL != (str = strchr(str, '.')))
{
/* do yer thing with str */
}
}

If you find that unclear, you might prefer to do it this way (it
amounts to the same thing):

void DoStuff(const char *str)
{
str = strchr(str, '.');
while(str)
{
/* do yer thing with str */
str = strchr(str, '.');
}
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
C

CBFalconer

Peter said:
.... snip ...

void DoStuff(const char *str)
{
str = strchr(str, '.');
while(str)
{
/* do yer thing with str */
str = strchr(str, '.');
}
}

That misses the essential characteristic of starting after the
'.'. He might try:

void DoStuff(const char *str)
{
while (str = strchr(str, '.') { /* found a period */
str++; /* skip over it */
/* do yer thing with str */
}
}
 
C

Christopher Benson-Manica

CBFalconer said:
void DoStuff(const char *str)
{
while (str = strchr(str, '.') { /* found a period */
str++; /* skip over it */
/* do yer thing with str */
}
}

That I like. Although I like to think my original implementation had
a certain twisted charm to it.
 

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

Similar Threads

trim whitespace v3 170
Comments please 36
Tricky double operations 5
printing double in binary 1
C language. work with text 3
String parsing question 16
Library bug or my fault? 65
validcstring function 12

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top