代码审查 [英] code review

查看:89
本文介绍了代码审查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;

}

}

返回t;

}

有没有办法改进上面的代码?

解决方案

" E. Robert Tisdale <,E ************** @ jpl.nasa.gov>。在消息中写道

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;
}
}
返回t;




你需要问? (为了新手的利益,ERT声称自己是专家./ />
在C.)


可以通过不经过两次字符串来改善它。 br />


On Fri,2004年2月6日13:04:43 -0800,E。 Robert Tisdale

< E. ************** @ 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;
}
}
返回t;
}



可能不是_much_的改进,但我注意到你做了两个

遍历源文本,一个在strcpy中,然后再次在你的

自己的循环中。这是一个通过的解决方案:


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)!=''\'''; src ++,dest ++)

* dest =(c ==''。'')? ''_'':c;

* dest =''\ 0'';

}


返回结果;

}

Leor Zolman

BD软件
le**@bdsoft.com
www.bdsoft.com - C / C ++,Java,Perl& A的现场培训Unix

C ++用户:下载BD Software的免费STL错误消息

Decryptor at www.bdsoft.com/tools/stlfilt.html


E. Robert Tisdale写道:

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 =''_'';
}
++你有没有办法改进上面的代码?




包含stdlib.h可能会有很大改善 - 我已经写了:b
$ b可能已经写了:


#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;

}

}

返回t;

}


但是

尺寸或性能无法确定任何真正的改善。

-

Morris Dovey
West Des Moines,爱荷华州美国

C链接在 http://www.iedu.com/c

读我的嘴唇:苹果离树不远。


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?

解决方案

"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.


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


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.


这篇关于代码审查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆