473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

code review

char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}
Is there any way to improve the code above?

Nov 14 '05 #1
26 2136
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Is there any way to improve the code above?


You need to ask? (For the benefit of newcomers, ERT claims to be an expert
on C.)

It can be improved by not going through the string twice.
Nov 14 '05 #2
On Fri, 06 Feb 2004 13:04:43 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Probably not _much_ of an improvement, but I noticed you make two
passes over the source text, one within strcpy and then again in your
own loop. Here's a one-pass solution:

char *dot_to_underscore2(const char *s)
{
char *result;

if ((result = malloc(strlen(s) + 1)) != NULL)
{
char const *src = s;
char *dest = result;
char c;

for (; (c = *src) != '\0'; src++, dest++)
*dest = (c == '.') ? '_' : c;
*dest = '\0';
}

return result;
}
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
E. Robert Tisdale wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}
Is there any way to improve the code above?


It would probably be much improved by including stdlib.h - I'd
probably have written:

#include <stdio.h>
char *dot_to_underscore(const char *s)
{ char c,*t=s,*u;
if (s && (t = u = malloc(strlen(s) + 1)))
{ while (c = *s++)
{ if (c == '.') c = '_';
*u++ = c;
}
}
return t;
}

but there's no way to be sure of any real improvement in either
size or performance.
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #4
An afterthought...

Probably be a good idea to include string.h, too.

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #5
"Morris Dovey" <mr*****@iedu.com> wrote in message
news:Ao****************@news.uswest.net...
E. Robert Tisdale wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}
Is there any way to improve the code above?
It would probably be much improved by including stdlib.h - I'd
probably have written:


So why don't you include it? ;-)
And while you are at that, you may also #include <string.h> (for strlen) ;-)
#include <stdio.h>
char *dot_to_underscore(const char *s)
{ char c,*t=s,*u;
if (s && (t = u = malloc(strlen(s) + 1)))
{ while (c = *s++)
{ if (c == '.') c = '_';
*u++ = c;
}
}
return t;
}

but there's no way to be sure of any real improvement in either
size or performance.


Have you ever heard about profilers? ;-)
(No, I don't even pretend to be serious in *this* thread.)
Nov 14 '05 #6
Peter Pichler wrote:
Have you ever heard about profilers? ;-)
(No, I don't even pretend to be serious in *this* thread.)


Peter...

Of course - but I haven't been able to find a /generic/ profiler
with any of the search engines available to me.

Now /that/ would be an interesting project...

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #7
"E. Robert Tisdale" wrote:

char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Is there any way to improve the code above?


Since "goodness" is multi-dimensional, "improve" is
ambiguous. However, one possible improvement would be to
combine the translation with the copy operation:

char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if (t != NULL) {
char *u = t;
for ( ; ; ++s) {
if ((*u++ = *s == '.' ? '_' : *s) == '\0')
break;
}
}
return t;
}

(The loop could, of course, be written more compactly
at some cost in readability.)

Because of the multi-dimensionality mentioned above, no
consideration will be given to any discussion of whether this
sort of thing represents an "improvement."

--
Er*********@sun.com
Nov 14 '05 #8
On Fri, 06 Feb 2004 16:18:17 -0600, Morris Dovey <mr*****@iedu.com>
wrote:
An afterthought...

Probably be a good idea to include string.h, too.


Is there some sort of guideline in this group that makes posting a
free-standing, self-explanatory little function using totally standard
lib calls, without the requisite #includes, into a major faux pas? I
mean, I get that some folks may not like E. Robert Tisdale for one
reason or another and need to bash him, but out of that context it
just seems ugly. In it too, for that matter.
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #9
Leor Zolman wrote:
On Fri, 06 Feb 2004 16:18:17 -0600, Morris Dovey <mr*****@iedu.com>
wrote:
An afterthought...

Probably be a good idea to include string.h, too.
Is there some sort of guideline in this group that makes posting a
free-standing, self-explanatory little function using totally standard
lib calls, without the requisite #includes, into a major faux pas?


No, but de facto faux pas it is, nevertheless. When I'm composing code for
posting on comp.lang.c, I generally err on the side of caution and try to
make it totally nitpick-proof. I know I don't always succeed because I have
all these scars. :-)
I
mean, I get that some folks may not like E. Robert Tisdale for one
reason or another and need to bash him, but out of that context it
just seems ugly. In it too, for that matter.


It's nothing to do with Tisdale. It's to do with accuracy. Yes, it may seem
a bit petty, and yes, sometimes we don't bother (and of course we wouldn't
do this for an obviously exegetic fragment), but it is useful because it
makes people more conscious - more careful - about what they're writing.

Personally, I've found that this has a /positive/ effect on my programming.
When I'm writing code and find myself taking a little shortcut, I think to
myself "what would comp.lang.c say?", and Do the Right Thing instead.

So, all in all, I think the nitpicking is a good thing, not a bad thing.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #10
Leor Zolman wrote:
Is there some sort of guideline in this group that makes posting a
free-standing, self-explanatory little function using totally standard
lib calls, without the requisite #includes, into a major faux pas? I
mean, I get that some folks may not like E. Robert Tisdale for one
reason or another and need to bash him, but out of that context it
just seems ugly. In it too, for that matter.


Leor...

Nothing to do with the OP - and I'm not much of a "basher". If
even a small and extremely simple program requires a standard
header, then the code is incomplete without it.

I made the same mistake that the OP made ( as Peter so kindly
pointed out. :-)

I try to remember that there are lurkers in the shadows - people
who are just starting to learn C and who need to understand that
inclusion of proper header files is a necessary part of writing C
code. My personal rule of thumb is that if I post a complete
function, then the required headers should be shown. If it's just
a fragment of a function, then the headers aren't needed.

At one time or another I think every single C regular has pointed
out that I've forgotten a necessary header - you'd think I could
get it right by now...

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #11
On Fri, 06 Feb 2004 17:02:45 -0600, Morris Dovey <mr*****@iedu.com>
wrote:
Leor Zolman wrote:
Is there some sort of guideline in this group that makes posting a
free-standing, self-explanatory little function using totally standard
lib calls, without the requisite #includes, into a major faux pas? I
mean, I get that some folks may not like E. Robert Tisdale for one
reason or another and need to bash him, but out of that context it
just seems ugly. In it too, for that matter.


Leor...

Nothing to do with the OP - and I'm not much of a "basher". If
even a small and extremely simple program requires a standard
header, then the code is incomplete without it.

I made the same mistake that the OP made ( as Peter so kindly
pointed out. :-)

I try to remember that there are lurkers in the shadows - people
who are just starting to learn C and who need to understand that
inclusion of proper header files is a necessary part of writing C
code. My personal rule of thumb is that if I post a complete
function, then the required headers should be shown. If it's just
a fragment of a function, then the headers aren't needed.

At one time or another I think every single C regular has pointed
out that I've forgotten a necessary header - you'd think I could
get it right by now...


Okay, I hear you (and Richard). Every time I select Agent's Send Now
menu option (or hit control-N, which I discovered by accident just a
few days ago is equivalent), I steel myself up for the possibility of
regretting it ;-). But after several months of posting, for better or
worse, I can state that there's not one single response to any of my
posts, even the seriously brain-dead ones, that I ended up feeling
"attacked" over. I wonder whether my having at least a little bit of
name recognition (and certainly not to everybody) has tempered what
might otherwise have been more acerbic reactions, but for whatever the
reason, I feel like I've dodged a lot of bullets.

As I commented in a private email to Greg Comeau a while back, I've
observed that each group has its own particular rhythms and "rules"
(formal or otherwise, e.g. the topic of this sub-thread). At times,
discerning those rhythms feels like trying to see the picture hidden
within one of those stereo grams (I've never yet managed to see
one...)
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #12
In article <40**************@jpl.nasa.gov>,
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}
Is there any way to improve the code above?


Yes.
Nov 14 '05 #13
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
I prefer an in-place methodology or from-to as in many <string.h> functions.
It removes the need for this code to be tied to an allocation system like
malloc. [Of course, there are pros and cons to this.]
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Is there any way to improve the code above?


As others have said, the strcpy is somewhat duplicitous. If you are going to
use it though, then using strchr() in the while condition may be faster on
some implementations, if the function is commonly used with long strings
where little or no substitution is required.

Stylistically, I also prefer...

for (u = t; *u; u++)
{
...
}

....to...

*u = t;
while (*u)
{
...
++u;
}

--
Peter
Nov 14 '05 #14
Leor Zolman wrote:
On Fri, 06 Feb 2004 17:02:45 -0600, Morris Dovey <mr*****@iedu.com>
wrote:
At one time or another I think every single C regular has
pointed out that I've forgotten a necessary header - you'd
think I could get it right by now...


Okay, I hear you (and Richard). Every time I select Agent's
Send Now menu option (or hit control-N, which I discovered by
accident just a few days ago is equivalent), I steel myself up
for the possibility of regretting it ;-). But after several
months of posting, for better or worse, I can state that
there's not one single response to any of my posts, even the
seriously brain-dead ones, that I ended up feeling "attacked"
over. I wonder whether my having at least a little bit of name
recognition (and certainly not to everybody) has tempered what
might otherwise have been more acerbic reactions, but for
whatever the reason, I feel like I've dodged a lot of bullets.


Good for you! I don't think there's a mean-spirited regular in
the bunch - although there's quite a range of opinion as to how
to best help people learn. The point to keep in mind is that even
when (perhaps especially when!) the criticism is particularly
sharp, it's because there's someone who's concerned about the
quality of education.

My motto (dating from my first week on comp.lang.c) has been
"Never pass up an opportunity to display your ignorance". I'll
have to admit that I had to do a Google search on your name to
understand your remark about name recognition. Rest assured that
your past accomplishments won't afford you a bit of protection
here! You'll still need to get it right.

One of my all-time favorite quips was in a response from Richard
Heathfield to a post from Dennis Ritchie in which Richard asked:
"And your C question was?". ROFLMAO every time I recall it. 8-D
As I commented in a private email to Greg Comeau a while back,
I've observed that each group has its own particular rhythms
and "rules" (formal or otherwise, e.g. the topic of this
sub-thread). At times, discerning those rhythms feels like
trying to see the picture hidden within one of those stereo
grams (I've never yet managed to see one...)


And this is, of course, exactly why we /strongly/ encourage
newcomers to lurk for a while. It really does help to take time
to puzzle out what all these strange people are really like
before engaging them in discussion of what they know they know
really well. That little bit of preparation can prevent a lot of
discomfort...

I've never been able to see the hidden pictures, too - that must
be another of my hidden talents. (-:
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #15
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:c0**********@titan.btinternet.com...
Leor Zolman wrote:
On Fri, 06 Feb 2004 16:18:17 -0600, Morris Dovey <mr*****@iedu.com>
wrote:
[Context reinserted from previous posts...]
> > char *t = malloc(strlen(s) + 1);
> It would probably be much improved by including stdlib.h
An afterthought...

Probably be a good idea to include string.h, too.


Is there some sort of guideline in this group that makes posting a
free-standing, self-explanatory little function using totally standard
lib calls, without the requisite #includes, into a major faux pas?


No, but de facto faux pas it is, nevertheless.


I just find it humorously ironic that an _uncasted_ malloc should be called
on the issue of failing to provide a declaration for malloc! <g>

I can't recall if Dan Pop's current position is that...

void *malloc();

....is sufficient when the supplied parameter on calling is of type size_t.
[I still think it isn't sufficient.]

--
Peter
Nov 14 '05 #16
E. Robert Tisdale wrote:

char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Is there any way to improve the code above?


I prefer to supply the target array from the calling function.
That way the function is more versatile, it can be used
with an automatic array as well as a mallocated one.
Also, it makes it more obvious that the calling function
has the responsibility of doing any freeing, if needed.

char *dot_to_underscore(char *s1, const char *s2)
{
char *const p1 = s1;

do {
*s1++ = (*s2 == '.' ? '-' : *s2);
} while (*s2++ != '\0');
return p1;
}
--
pete
Nov 14 '05 #17
Morris Dovey wrote:
E. Robert Tisdale wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Is there any way to improve the code above?
It would probably be much improved by including stdlib.h - I'd
probably have written: ^^^^^^^^


Harrumph - ITYM also string.h. How could that consumate expert
Trollsdale make such beginners errors?

#include <stdio.h>
char *dot_to_underscore(const char *s)
{ char c,*t=s,*u;
if (s && (t = u = malloc(strlen(s) + 1)))
{ while (c = *s++)
{ if (c == '.') c = '_';
*u++ = c;
} *u = '\0'; /* <-------- */ }
return t;
}

but there's no way to be sure of any real improvement in either
size or performance.


You don't consider the fact that yours works and is readable an
improvement? Woops - see above addition.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #18
Leor Zolman wrote:
Morris Dovey <mr*****@iedu.com> wrote:
An afterthought...

Probably be a good idea to include string.h, too.


Is there some sort of guideline in this group that makes posting
a free-standing, self-explanatory little function using totally
standard lib calls, without the requisite #includes, into a major
faux pas? ... snip ...


Yes, by tradition. Especially honored for ERT. One learns to
guard ones rear from the attack dogs by including such caviling
clauses as "assuming suitable includes" etc. :-)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #19
CBFalconer wrote:
You don't consider the fact that yours works and is readable an
improvement? Woops - see above addition.


Woops, indeed.

One more time, to see if I can get the header files right, add
the string terminator, and remove some of the clutter (braces)
that interferes with readability (-:

#include <stdlib.h>
#include <string.h>
char *dot_to_underscore(const char *s)
{ char *t=s,*u;
if (s && (t = u = malloc(strlen(s) + 1)))
do *u++ = (*s == '.') ? '_' : *s;
while (*s++);
return t;
}

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #20
In 'comp.lang.c', "E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
You must be a fake of ERT.
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Is there any way to improve the code above?


Just a proposal:

#include <string.h>

char *dot_to_underscore_dyn (const char *const s)
{
char *t = NULL;

if (s != NULL)
{
size_t const size = strlen (s) + 1;

t = malloc (size);

if (t != NULL)
{
char *u = t;
memcpy(t, s, size);

while (*u)
{
if (*u == '.')
{
*u = '_';
}
++u;
}
}
}
return t;
}
--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #21
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}
Is there any way to improve the code above?


/* Saturday-morning-too-much-coffee-version ;-) */

#include <stdlib.h>
#include <string.h>

char *dot_to_underscore( const char *s )
{
char *res = malloc( strlen( s ) + 1 );

if ( res != NULL )
{
char *p = res;
while ( *p++ = *s == '.' ? '_' : *s )
s++;
}
return res;
}
Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #22
E. Robert Tisdale wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}
Is there any way to improve the code above?
Assuming the following inclusions:

#include <stdlib.h>
#include <string.h>

I might write:

inline
char* dot_to_underscore(const char* s) {
char* const t = (char*)malloc(strlen(s) + 1);
if(NULL != t) {
char *u = t;
while('\0' != (*u = *s)) {
if ('.' == *u)
*u = '_';
++u;
++s;
}
}
return t;
}

and include it in my main.c source file with:

#include <stdio.h>

int main(int argc, char* argv[]) {
const char* const t = dot_to_underscore(argv[1]);
fprintf(stdout, "%s\n", t);
free((void*)t);
return 0;
}
gcc -Wall -std=c99 -pedantic -O2 -o main main.c
./main 'users.powernet.co.uk'

users_powernet_co_uk

Nov 14 '05 #23
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
<snip>
E. Robert Tisdale wrote:
<code snipped>
Is there any way to improve the code above?


Assuming the following inclusions:

#include <stdlib.h>
#include <string.h>

I might write:

inline
char* dot_to_underscore(const char* s) {
char* const t = (char*)malloc(strlen(s) + 1);

Unnecessary cast of malloc return value. *SCNR*
if(NULL != t) {
char *u = t;
while('\0' != (*u = *s)) {
if ('.' == *u)
*u = '_';
++u;
++s;
}
}
return t;
}

and include it in my main.c source file with:

#include <stdio.h>

int main(int argc, char* argv[]) {
const char* const t = dot_to_underscore(argv[1]);
Undefined behavior, if program is invoked without argument(s).
fprintf(stdout, "%s\n", t);
free((void*)t);
return 0;
}


<OT>
> gcc -Wall -std=c99 -pedantic -O2 -o main main.c
> ./main 'users.powernet.co.uk'

users_powernet_co_uk


Status of C99 inline function support is "Broken" for current
releases of gcc, according to gnu.org (modulo caching errors).

Do not resort to an inferior implementation, when you want to
use certain C99 features in your code.
</OT>

Now, what an improvement ...

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #24
Morris Dovey wrote:
CBFalconer wrote:
You don't consider the fact that yours works and is readable an
improvement? Woops - see above addition.

Woops, indeed.

One more time, to see if I can get the header files right, add the
string terminator, and remove some of the clutter (braces) that
interferes with readability (-:

#include <stdlib.h>
#include <string.h>
char *dot_to_underscore(const char *s)
{ char *t=s,*u;
if (s && (t = u = malloc(strlen(s) + 1)))
do *u++ = (*s == '.') ? '_' : *s;
while (*s++);
return t;
}


Not being an expert or anything, but wouldn't it be better to change
char *t=s,*u;
to
char *t=NULL,*u;

I believe the result is the same, because t will be NULL if s == NULL
and if malloc fails, just as in the original code, but if compiled with:
gcc -c -W -Wall -ansi -pedantic dot_to_underscore.c

it will suppress:
dot_to_underscore.c: In function `dot_to_underscore':
dot_to_underscore.c:5: warning: initialization discards qualifiers from
pointer target type

Mark
--
<<Remove the del for email>>

Nov 14 '05 #25
E. Robert Tisdale wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}
Is there any way to improve the code above?

Incredible. I didn't recognise this code! But it's mine all right. Mr
Tisdale found it at http://users.powernet.co.uk/eton/c/emgen.c

Thanks for the suggestions for improvements, folks.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #26
Richard Heathfield <do******@address.co.uk.invalid> scribbled the following:
E. Robert Tisdale wrote:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Is there any way to improve the code above?
Incredible. I didn't recognise this code! But it's mine all right. Mr
Tisdale found it at http://users.powernet.co.uk/eton/c/emgen.c Thanks for the suggestions for improvements, folks.


Algorithmically, the function is so trivial that I thought Tisdale had
come up with the same implementation as you by accident, but then I
checked your web site. It matches your code's syntax and writing style
exactly to the last character. Now there is no doubt. Tisdale used your
code without saying it was yours.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Bad things only happen to scoundrels."
- Moominmamma
Nov 14 '05 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Arvie | last post by:
I need some advice guys.. I am proposing that we get someone to do a complete audit/review of our Java application codebase, about 1000 JSPs/Servlets and 100 EJBs. If I get firms to submit...
0
by: gs-code-review-bounces | last post by:
Your mail to 'gs-code-review' with the subject Re: Application Is being held until the list moderator can review it for approval. The reason it is being held: Post by non-member to a...
18
by: Ben Hanson | last post by:
I have created an open source Notepad program for Windows in C++ that allows search and replace using regular expressions (and a few other extras). It is located at...
19
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
3
by: Filippo | last post by:
Hi, In my organization we would like to activate a code review system, in wich a developer have to pass a review from a reviewer before check in the modified files in source safe. Is there any way...
239
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my...
3
by: JeanDean | last post by:
I am looking for freeware tool which can review the c++ code(compiled on g++). Please share your experiences and details obout the usage of the tool.
10
by: Jo | last post by:
Hi there: I m wondering what can I do to improve my code, everytime I am coding I feel like it could be done better. I started on c# a good months ago and feel conformtable but sometimes I Need...
4
maxx233
by: maxx233 | last post by:
Hello all, I'm new to OO design and have a question regarding where I should place some code. Here's a simplified situation: I'm making an app to do create, submit, and track employee reviews...
0
by: corey | last post by:
Secure Bytes audit and vulnerability assessment software Secure Auditor named “Versatile tool” and earn “Five Star Ratings” in SC Magazine Group Test Secure Bytes is really pleased to share this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.