反转并打印一个字符串 [英] Reverse and print a string

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

问题描述

我(递归)如何反转和打印字符串(简单如

)?我尝试使用以下代码,但它不起作用。谢谢

提前给大家。

/ *代码从这里开始* /


#include< stdio.h>


void invstr(char * s){


/ *基本情况* /

if((* s )==''\''')

返回;

else {/ *一般情况* /

invstr(s ++);

printf("%c",*(s));

}

}


int main(int argc,char * argv []){

char * miastringa =" CIAO";

invstr(miastringa);

system(PAUSE); / *因为Dev-C ++ IDE * /

返回0;

}


/ *代码在这里结束* /

解决方案

nembo kid写道:


我如何(递归)可以反转和打印字符串(简单如

)?我尝试使用以下代码,但它不起作用。非常感谢

提前给大家。


/ *代码从这里开始* /


#include< stdio .h>


void invstr(char * s){


/ *基本情况* /

如果((* s)==''\''')

返回;

else {/ *一般情况* /

invstr (s ++);

printf("%c",*(s));

}



's ++`increments`s`。你不想这样做;你只想要

来传递`s + 1`。


-

"整个装置看起来很像已经被投入/杰克的老鹰/

以及最疯狂的匆忙狂热的技术人员可以鼓起勇气。


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰


nembo kid写道:


我如何(递归)可以反转和打印字符串(简单如

)?



[...]


/ *反向打印一个字符串。调用为:程序字符串* /

#include< stdio.h>

#include< string.h>


int main(int argc,char ** argv)

{

const long len = strlen(argv [1]);

long ctr;


for(ctr = len-1; ctr> = 0; ctr--)putc(argv [1] [ctr],stdout);

putc(''\''',stdout);

返回0;

}


Chris Dollin ha scritto:


`s ++`increments`s`。你不想这样做;你只想要

来传递`s + 1`。



哦对了!完美


所以''''''''''''''''''''''''




/ p>

How I (recursive) can reverse and print a string (as simple as
possible)? I tried with the following code, but it doesn''t work. Thanks
in advance to everbody.
/* Code starts here */

#include <stdio.h>

void invstr(char* s) {

/* base case */
if ((*s) == ''\0'')
return;
else { /* general case */
invstr(s++);
printf("%c", *(s));
}
}

int main (int argc, char* argv[]) {
char* miastringa="CIAO";
invstr(miastringa);
system("PAUSE"); /* Because of Dev-C++ IDE */
return 0;
}

/* Code ends here */

解决方案

nembo kid wrote:

How I (recursive) can reverse and print a string (as simple as
possible)? I tried with the following code, but it doesn''t work. Thanks
in advance to everbody.
/* Code starts here */

#include <stdio.h>

void invstr(char* s) {

/* base case */
if ((*s) == ''\0'')
return;
else { /* general case */
invstr(s++);
printf("%c", *(s));
}

`s++` increments `s`. You don''t want to do that; you just want
to pass `s+1`.

--
"The whole apparatus had the look of having been put /Jack of Eagles/
together with the most frantic haste a fanatically
careful technician could muster."

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


nembo kid wrote:

How I (recursive) can reverse and print a string (as simple as
possible)?

[ ... ]

/* Print a string in reverse. Invoke as: Program string */
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
const long len = strlen(argv[1]);
long ctr;

for (ctr = len-1; ctr >= 0; ctr--) putc(argv[1][ctr], stdout);
putc(''\0'', stdout);
return 0;
}


Chris Dollin ha scritto:

`s++` increments `s`. You don''t want to do that; you just want
to pass `s+1`.

Oh right! perfect

So ''s++'' it''s not same as ''s+1''


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

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