E
Emmanuel Delahaye
E. Robert Tisdale said: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;
}