C风格,一个参数,递归,字符串反转 [英] C style , one argument, recursive, string reverse

查看:93
本文介绍了C风格,一个参数,递归,字符串反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想用ac风格的字符串反转器,我想要它

拿1个参数

Altough I在现实生活中永远不会这样做。有办法吗?


我写的这个功能失败了:


知道为什么会失败吗?


char * C_recReverse(char * str)

{

char * sub,* res,* tmp;

if(strlen(str)== 0)

return";

else

{

sub =(char *)malloc(sizeof(char)* strlen(str)-1);

res =(char *)malloc(sizeof(char)* strlen(str));

strcpy(sub,str);

sub [strlen(str)-1] =''\''';

cout<< ;" Calling with:"<<& str [strlen(str)-1]<<" \t And \t"<< sub<< endl;

strcpy(res,"");

strncpy(res,& str [strlen(str)-1],1);

tmp = C_recReverse(sub);

strcat(res,tmp);

res [strlen(str)] =''\ 0'';

cout<<" res:"<< res<< endl;

free(sub);

free(tmp);

返回res;

} < br $>

}

谢谢。

解决方案

zahy [dot ] bnaya [AT]的Gmail [点] com" < za ******** @ gmail.com>


您的垃圾邮件警卫不起作用,因为实际地址是明文。很好

尝试,但放弃守卫GMail的垃圾邮件。我发布了我的原始地址,每天获得> 100

垃圾邮件。谷歌...谷歌搜索他们,并很好地保留98%的他们的收件箱
我的收件箱。道具到谷歌!

我正在尝试用ac风格的字符串反向器,


考试,对吧?

知道它失败的原因吗?


从头开始再写一次。有时当某些事情失败时,再次开始

。并且要简单得多。

char * C_recReverse(char * str)


返回无效。你应该在原地反转str

sub =(char *)malloc(sizeof(char)* strlen(str)-1);



$ b除非你必须,否则永远不会分配任何东西。


试试这个算法:


void reverse(char * str,int len)

如果0> = len返回

swap str [0],str [len-1]

reverse(str + 1,len - 2 )


你现在看到你的想法正在逆转吗?


-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - - 不是博客!!!




Phlip写道:

我正在尝试用ac风格的字符串反转器,
考试,对吗?




其实没有,我在求职面试中得到了它,我本能地尝试了递归式的

然后我来了我的感觉并且做了一个循环:)
< br>

char * C_recReverse(char * str)



返回无效。你应该就地反转str

sub =(char *)malloc(sizeof(char)* strlen(str)-1);



尝试此算法:

void reverse(char * str,int len)
如果0> = len返回
swap str [0],str [len-1]
反向(str + 1,len - 2)

你现在看到你心中的逆转了吗? / blockquote>


我正在尝试这样的事情:

string recReverse(string str)

{

if(str.empty())

返回" ;;

其他

返回

str.substr(str.length() - 1,str.length())。append(recReverse(str.substr

(0,str.length() - 1))) ;

}


对于丑陋的一行退货声明很抱歉..但这是我想要的b $ b想要做的事情用C风格的字符串。


Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客!!!




谢谢。


zahy [dot] bnaya [at] gmail [dot] com发布:


我正在努力想出一个ac风格的弦乐反向器,我想要它采取一个论点
Altough我永远不会在现实生活中这样做。有办法吗?




#include< algorithm>

#include< cstring>


void ReverseString(char * const p)

{

std :: reverse(p,p + std :: strlen(p));

}

或者如果您不想要标准库的帮助......


未经测试的代码:

void ReverseString(注册char * p_begin)

{

注册char * p_end = p_begin;


while(* p_end ++);


--p_end;

if(p_begin == p_end)return;

for(; ; ++ p_begin, - p_end)

{

开关(p_end - p_begin)

{

case 1:

案例2:

{

char temp = * p_begin;

* p_end = * p_begin;

* p_begin = temp;

返回;

}

}

}

}


-Tomás


Hi,
I am trying to come up with a c style string reverser, I want it to
take 1 argument
Altough I would never do this in real life. Is there a way to do it?

I wrote this function that fails :

Any idea why it fails?

char * C_recReverse(char * str)
{
char* sub, *res, *tmp;
if (strlen(str) == 0)
return "";
else
{
sub = (char*)malloc(sizeof(char)*strlen(str)-1);
res = (char*)malloc(sizeof(char)*strlen(str));
strcpy(sub,str);
sub[strlen(str)-1] = ''\0'';
cout<<"Calling with :"<<&str[strlen(str)-1]<<"\t And \t"<<sub<<endl;
strcpy(res,"");
strncpy(res,&str[strlen(str)-1],1);
tmp = C_recReverse(sub);
strcat(res,tmp);
res[strlen(str)] = ''\0'';
cout<<"res:"<<res<<endl;
free(sub);
free(tmp);
return res;
}

}
Thanks.

解决方案

zahy[dot]bnaya[At]gmail[dot]com" <za********@gmail.com>

Your spam guard won''t work because the actual address is in clear text. Nice
try, but give up spam guarding GMail. I post my raw address, and get >100
spams per day. Google ... Googles them, and nicely keeps 98% of them out of
my Inbox. Props to Google!

I am trying to come up with a c style string reverser,
Exam, right?
Any idea why it fails?
Write it again, from scratch. Sometimes when something fails, just start
again. And get much less complex.
char * C_recReverse(char * str)
Return void. You should reverse str in-place
sub = (char*)malloc(sizeof(char)*strlen(str)-1);



And never allocate anything unless you must.

Try this algorithm:

void reverse(char * str, int len)
if 0 >= len return
swap str[0], str[len-1]
reverse(str + 1, len - 2)

Do you see the reversing in your mind now?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!



Phlip wrote:

I am trying to come up with a c style string reverser,
Exam, right?



Actually no, I got it on a job interview, I instinctively tried
the recursive style then I
came to my senses and did it with a loop :)

char * C_recReverse(char * str)



Return void. You should reverse str in-place

sub = (char*)malloc(sizeof(char)*strlen(str)-1);



And never allocate anything unless you must.

Try this algorithm:

void reverse(char * str, int len)
if 0 >= len return
swap str[0], str[len-1]
reverse(str + 1, len - 2)

Do you see the reversing in your mind now?



I am trying to do something like this:
string recReverse(string str)
{
if(str.empty())
return "";
else
return
str.substr(str.length()-1,str.length()).append(recReverse(str.substr
(0,str.length()-1)));
}

Sorry for the ugly one line return statement.. but this is the idea I
am trying to do in C style strings.

Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!



Thanks.


zahy[dot]bnaya[At]gmail[dot]com posted:

Hi,
I am trying to come up with a c style string reverser, I want it to
take 1 argument
Altough I would never do this in real life. Is there a way to do it?



#include <algorithm>
#include <cstring>

void ReverseString( char * const p )
{
std::reverse( p, p + std::strlen(p) );
}
Or if you don''t want the help of the Standard Library...

Untested code:
void ReverseString( register char *p_begin )
{
register char *p_end = p_begin;

while ( *p_end++ );

--p_end;
if ( p_begin == p_end ) return;
for( ; ; ++p_begin, --p_end )
{
switch( p_end - p_begin )
{
case 1:
case 2:
{
char temp = *p_begin;
*p_end = *p_begin;
*p_begin = temp;
return;
}
}
}
}

-Tomás


这篇关于C风格,一个参数,递归,字符串反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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